java按字节截取中英文混合字符串

来源:互联网 发布:linux中chmod -r 777 编辑:程序博客网 时间:2024/05/30 23:53
public String subStringByByte(String str, int len){
String result = null;
if(str != null){
byte[] a = str.getBytes();
if(a.length <= len){
result = str;
}else if(len > 0){
result = new String(a, 0, len);
System.out.println(result);
int length = result.length();
if(str.charAt(length - 1) != result.charAt(length-1)){
if(length < 2){
result = null;
}else{
result = result.substring(0, length - 1);
}
}
}
}
return result;
}