您的位置:首页 > 移动开发 > Android开发

android 字符长度限制

2017-12-20 14:24 155 查看
public static String handleText(String str, int maxLen) {
if (TextUtils.isEmpty(str)) {
return str;
}
int count = 0;
int endIndex=0;
for (int i = 0; i < str.length(); i++) {
char item = str.charAt(i);
if (item < 128) {
count = count + 1;
} else {
count = count + 2;
}
if(maxLen==count || (item>=128 && maxLen+1==count)){
endIndex=i;
}
}
if (count <= maxLen) {
return str;
} else {

return str.substring(0, endIndex) + "...";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android string
相关文章推荐