/**
* 截取字符串,输入为一个字符串或字节数,输出为按字节截取的字符串,但要保证汉字不能截取半个
* 如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
*/
public String splitString(String str, int length) {
String resultStr = null;
byte[] strBytes = str.getBytes();
int byteLength = strBytes.length;
if (length > byteLength) {
length = byteLength;
}
int splitLen = length;
while (strBytes[splitLen-1] < 0) {
--splitLen;
}
resultStr = new String(strBytes, 0, splitLen);
return resultStr;
}
截取字符串,输入为一个字符串或字节数,输出为按字节截取的字符串,但要保证汉字不能截取半个
最新推荐文章于 2022-03-12 20:23:53 发布