求解!!!!!!!!!!!!!!!!!!!!!!! 今天看很多关于这个问题都答案不正确 所以请高手解答编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我倒, Java里面不管你是汉字还是其它任何东东, 都是一个字符...Unicode, 好好看看书吧我ABC这样的长度就是3 package string;class SplitString{ private String str; private int byteNum; public SplitString(){} public SplitString(String str,int byteNum) { this.str=str; this.byteNum=byteNum; } public void splitIt() { byte bt[]=str.getBytes(); System.out.println("Length of this String ===>"+bt.length); if(byteNum>1) { if(bt[byteNum]<0) { String substrx=new String(bt,0,--byteNum); System.out.println(substrx); }else { String substrex=new String(bt,0,byteNum); System.out.println(substrex); } }else { if(byteNum==1) { if(bt[byteNum]<0) { String substr1=new String(bt,0,++byteNum); System.out.println(substr1); }else { String subStr2=new String(bt,0,byteNum); System.out.println(subStr2); } }else { System.out.println("输入错误!!!请输入大于零的整数:^v供Bti5教In网+X网G%*网%"教z32Fx8#M5(i,zT}H22{dD的n"); } } }}public class TestSplitString{ public static void main(String args[]) { String str="我ABC汉DEF"; int num=6; SplitString sptstr = new SplitString(str,num); sptstr.splitIt(); }} String str = "我ABC汉DEF"; final char beginChar = '\u4E00', endChar = '\u9FA5'; int index = 0, required = 6; int len = 0; while(len < str.length()){ char c = str.charAt(len); if(c >= beginChar && c <= endChar) index +=2; else index ++; if(index > required) break; len++; } System.out.println(str.substring(0, len)); 这个题目在java里是不存在半个的情况的,我也怀疑出题人脑子是不是进水!LZ是不是看了某培训机构的面试宝点哦?和我看的一样! 我到是想了很久用什么方法去截取半个汉字!getbytes转化为bit类型再截取一半再new String(byte []) 应该也是乱码! java文件运行问题 char可以赋值为单个中文字吗? main线程和main调用的线程,JVM的执行这两个线程是无序的??(附代码) 有关JSP问题 30分求:用jbuilder编写的计算器,带帮助文档的 强型别参数是什么意思 一些基础的问题(1) 请问,如何用JAVA得到当前系统屏幕的分辨率? 将数据库某一个表的几行复制到另一个表,应该用怎样的sql语句(java jdbc)(急) 请问大家在哪能下载 weblogic 谢谢 过时API 我的数组元素怎么引用啊?
这样的长度就是3
class SplitString
{
private String str;
private int byteNum; public SplitString(){} public SplitString(String str,int byteNum)
{
this.str=str;
this.byteNum=byteNum; }
public void splitIt()
{ byte bt[]=str.getBytes();
System.out.println("Length of this String ===>"+bt.length);
if(byteNum>1)
{
if(bt[byteNum]<0)
{
String substrx=new String(bt,0,--byteNum);
System.out.println(substrx);
}else
{
String substrex=new String(bt,0,byteNum);
System.out.println(substrex);
}
}else
{
if(byteNum==1)
{
if(bt[byteNum]<0)
{
String substr1=new String(bt,0,++byteNum);
System.out.println(substr1);
}else
{
String subStr2=new String(bt,0,byteNum);
System.out.println(subStr2);
}
}else
{
System.out.println("输入错误!!!请输入大于零的整数:^v供Bti5教In网+
X网G%*网%"教z32Fx8#M5(i,zT}H22{dD的n");
}
}
}
}public class TestSplitString
{
public static void main(String args[])
{
String str="我ABC汉DEF";
int num=6;
SplitString sptstr = new SplitString(str,num);
sptstr.splitIt();
}
}
final char beginChar = '\u4E00', endChar = '\u9FA5';
int index = 0, required = 6;
int len = 0;
while(len < str.length()){
char c = str.charAt(len);
if(c >= beginChar && c <= endChar)
index +=2;
else index ++;
if(index > required)
break;
len++;
}
System.out.println(str.substring(0, len));