Posted on 2008-02-25 11:38
angel 阅读(318)
评论(0) 编辑 收藏
如:abc中国
package com;
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();
if (bytenum > 1) {
if(bt[bytenum]<0){
String str=new String(bt,0,--bytenum);
System.out.print(str);
}else{
String str=new String(bt,0,bytenum);
System.out.print(str);
}
} else {
if(bytenum==1){
if(bt[bytenum]<0){
String str=new String(bt,0,++bytenum);
System.out.print(str);
}else{
String str=new String(bt,0,bytenum);
System.out.print(str);
}
}else{
System.out.print("错误");
}
}
}
}
public class Test {
public static void main(String arg[]) {
String str = "我abcd汉def";
int num = 7;
splitstring sptstr = new splitstring(str, num);
sptstr.splitit();
}
}
输出为:我abcd