如: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























































