/** * 根据输入的字符串计算出其长度,支持中文和全角字符 * @param str * @return */ public static Integer length(String str){ char[] chars_ss = str.toCharArray(); Integer StringLength = 0; for (int i = 0; i < chars_ss.length; i++) { String temp = String.valueOf(chars_ss[i]); Integer charLength = 1; // 判断是全角字符 // "[u4e00-u9fa5]" 中文字符 if (temp.matches("[^\x00-\xff]")) { charLength = 2; } StringLength += charLength; } return StringLength; }