class CharaGet{
private char [] cArr;
private String newStr="";
private int tempLen, temp;
public String getString(String str ,int len){
if(len<=0){
// should throw exception
System.out.println("illegal length the length must above 0");
}
// change to char[]
cArr =str.toCharArray();
for(int i =0; i<len; i ++ )
{ // the length of every character
temp =(cArr[i]+"").getBytes().length;
// total length
tempLen =tempLen+temp;
// test
System.out.println(tempLen+ "=" +cArr[i] + "第 "+i +"个" );
if (tempLen >len ) break;
newStr= newStr+cArr[i];
}
return newStr;
}
}
public class Test1{
public static void main(String args[]){
System.out.println( new CharaGet().getString("我们的ABC和他们的ABC", 8));
}
}