您好,欢迎来到外链网!
当前位置:外链网 » 站长资讯 » 专业问答 » 文章详细 订阅RssFeed

英文字符和中文字符长度,一个英文字符和一个中文字符的区别

来源:互联网 浏览:68次 时间:2023-04-08

比如最大10个字符,若超过10个,则显示9或者8个字符加省略号.

啊啊啊啊啊

哎哎哎啊...

哎哎哎啊a...

aaaaaaaa...


public class CalTextLength {
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) + "..."; }} }


private static final int maxLen=12; //名字最大字符数tring name=CalTextLength.handleText(simpleContact.getName(),maxLen);holder.tv_name.setText(name);

04946789