/**编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但
是要保证汉字不被截半个,如“我ABC” 4,应该截为“我AB”,输入“我ABC汉DEF” 6,应该
输出“我ABC”,而不是“我ABC+汉的半个”。**/
import java.util.Scanner;
public class InterceptionStr{
static String ss; ////要进行截取操作的字符串
static int n; ////截取的字符串的字节数
public static void main(String[] args){
System.out.println("请输入字符串");
Scanner scStr=new Scanner(System.in);
ss=scStr.next();
System.out.println("请输入字节数");
Scanner scByte=new Scanner(System.in);
n=scByte.nextInt();
Interception(setValue()); ///方法与方法中的套用
}
public static String[] setValue(){
String[] string=new String[ss.length()];
for(int i=0;i<string.length;i++){
string[i]=ss.substring(i,i+1);
}
return string;
}
public static void Interception(String[] string){
int count=0;
String m="[\u4e00-\u9fa5]"; ////汉字的正则表达式
System.out.println("以每"+n+"字节划分的字符串如下所示:");
for(int i=0;i<string.length;i++){
if(string[i].matches(m)){
count=count+2;
}else{
count=count+1;
}
if(count<n){
System.out.print(string[i]);
}else if(count==n){
System.out.print(string[i]);
count=0;
System.out.println();
}else{
count=0;
System.out.println();
}
}
}
}
- 大小: 15.4 KB
分享到:
相关推荐
string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子...
SQL截取字符串函数(1、截取从字符串左边开始N个字符 2、截取从字符串右边开始N个字符(例如取字符www.163.com) 3、截取字符串中任意位置及长度 等等)
oracle,按特定字符,截取字符串,直接出结果 oracle,按特定字符,截取字符串,直接出结果oracle,按特定字符,截取字符串,直接出结果
C# 按照字节长度截取字符串,可以截取任意字节长度得字符串,使用了一点正则表达式。
用SQL语句截取字符串, string substr ( string string, int start [, int length]) 参数1:处理字符串 参数2:截取的起始位置(第一个字符是从0开始) 参数3:截取的字符数量
bat截取字符串
js截取字符串常用的方法很多,你知道多少呢?看看String和StringBuffer如何截取字符串的
C#经常用到的几个截取字符串1 取字符串的前i个字符2 去掉字符串的前i个字符:3 从右边开始取i个字符:
ASP截取字符串(既:利用asp代码控制字符的显示)
jstl和struts2标签截取字符串 jstl(JavaServer Pages Standard Tag Library)是一种用于Java Web开发的标签库,提供了多种有用的标签来简化JSP页面的开发。其中,jstl的标签可以用来截取字符串,本文将详细介绍...
CString截取字符串全攻略 - fox的日志 - 网易博客.mht
适合刚刚进入(JavaScript,Vue)编程领域的小白使用
在VC++编程中,我们有时会遇到关于截取字符串的问题,对于CString类型字符串的截取VC中有现成的函数可供使用,主要是:CString Mid (int nFirst, int nCount);和BOOL AfxExtractSubString (CString& rString, ...
在SQL查询中如何截取字符串后 转成数值型?
delphi 实现截取字符串中中文+英文混合截取,当字符串中包含中文和英文支付时,有时候会将一个中文截取1半,出现乱码。该demo能很好的解决该问题
asp.net截取字符串方法 使用字节精确计算 计算空格 直接复制使用 简单
js截取字符串函数.txt
Java精确截取字符串
在js中,截取字符串,往往用substring,可另外,还有slice也是为截取字符串