extends:http://www.tuicool.com/articles/EB36Jv
public static int calculateLength(String etString) { char[] ch = etString.toCharArray(); int varlength = 0; for (int i = 0; i < ch.length; i++) { // changed by zyf 0825 , bug 6918,加入中文标点范围 , TODO 标点范围有待具体化 if ((ch[i] >= 0x2E80 && ch[i] <= 0xFE4F) || (ch[i] >= 0xA13F && ch[i] <= 0xAA40) || ch[i] >= 0x80) { // 中文字符范围0x4e00 0x9fbb varlength = varlength + 2; } else { varlength++; } } Log.d("TextChanged", "varlength = " + varlength); // 这里也可以使用getBytes,更准确嘛 // varlength = etstring.getBytes(CharSet.forName("GBK")).lenght;// 编码根据自己的需求,注意u8中文占3个字节... return varlength; }
时间: 2024-12-24 07:29:17