如何实现java中英文混合字符截取 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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("输入错误!!!请输入大于零的整数:"); } } } } public class TestSplitString { public static void main(String args[]) { String str="我ABC汉DEF"; int num=6; SplitString sptstr = new SplitString(str,num); sptstr.splitIt(); } } bt[byteNum] < 0是什么意思啊 bt[byteNum] < 0是什么意思啊是不是说哪个字节是一半呀? 还有就是哪个代码可以编译但是运行的结果不对呀 如何将list转成数组? 如何用htmlparser 取出table中的数据 考试题请教! 有没有办法以读写的方式打开文件? 有没有什么方法可以使自己写的class不被别人反编译呀? 大家快来帮帮我...... 字节码怎么理解??? 关于JDK 和JB?? 怎么把double转化为java.lang.Double?!! 内部比较器怎么调用compareTo方法 final使用时的疑问? 在Java中,怎样把一个图片文件写入到oracle数据库中,怎样读出来显示?
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("输入错误!!!请输入大于零的整数:");
}
}
}
} public class TestSplitString
{
public static void main(String args[])
{
String str="我ABC汉DEF";
int num=6;
SplitString sptstr = new SplitString(str,num);
sptstr.splitIt();
}
}
是不是说哪个字节是一半呀?
还有就是哪个代码可以编译但是运行的结果不对呀