編寫一個截取字符串的函數,輸入爲一個字符串和字節數,輸出爲按字節截取的字符串

 編寫一個截取字符串的函數,輸入爲一個字符串和字節數,輸出爲按字節截取的字符串。但是要保證漢字不被截半個,
如“我ABC”4,應該截爲“我AB”,輸入“我ABC漢DEF”,6,應該輸出爲“我ABC”而不是“我ABC+漢的半個

class SplitString {
String SplitStr;
int SplitByte;
public SplitString(String str,int bytes) {
SplitStr=str;
SplitByte=bytes;
System.out.println("The String is:′"+SplitStr+"′;SplitBytes="+SplitByte);
}
public void SplitIt() {
int loopCount;
loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):
(SplitStr.length()/SplitByte+1);
System.out.println("Will Split into "+loopCount);
for (int i=1;i<=loopCount ;i++ ) {
if (i==loopCount){
System.out.println(SplitStr.substring((i-1)*SplitByte,SplitStr.length()));
} else {
System.out.println(SplitStr.substring((i-1)*SplitByte,(i*SplitByte)));
}
}
}
public static void main(String[] args) {
SplitString ss = new SplitString("test中dd文dsaf 中男大3443n中國43中國0ewldfls=103",4);
ss.SplitIt();
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章