Java 计算中英文长度的若干种方法
2014-08-12 19:18
204 查看
在项目开发中经常碰到到输入字符的校验,特别是中英文混合在一起的校验。而为了满足校验的需求,有时需要计算出中英文的长度。
本文将通过几种常用的方法实现长度的计算:
本文将通过几种常用的方法实现长度的计算:
<span style="font-size:18px;">import java.io.UnsupportedEncodingException; /** * 中英文校验的处理 * @author a123demi * */ public class EnChValidate { public static void main(String[] args){ String validateStr= "中英文校验abcde接口ii"; int bytesStrLength = getBytesStrLength(validateStr); int chineseLength = getChineseLength(validateStr); int regexpLength = getRegExpLength(validateStr); System.out.println("getBytesLength:" + bytesStrLength + ",chineseLength:" + chineseLength + ",regexpLength:" + regexpLength); } /** * 根据字符编码 字节数生成一个临时的字符串 * @param validateStr * @return */ public static int getBytesStrLength(String validateStr){ String tempStr = ""; try { tempStr = new String(validateStr.getBytes("gb2312"),"iso-8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tempStr.length(); } /** * 获取字符串的长度,如果有中文,则每个中文字符计为2位 * * @param validateStr * 指定的字符串 * @return 字符串的长度 */ public static int getChineseLength(String validateStr) { int valueLength = 0; String chinese = "[\u0391-\uFFE5]"; /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */ for (int i = 0; i < validateStr.length(); i++) { /* 获取一个字符 */ String temp = validateStr.substring(i, i + 1); /* 判断是否为中文字符 */ if (temp.matches(chinese)) { /* 中文字符长度为2 */ valueLength += 2; } else { /* 其他字符长度为1 */ valueLength += 1; } } return valueLength; } /** * 利用正则表达式将每个中文字符转换为"**" * 匹配中文字符的正则表达式: [\u4e00-\u9fa5] * 匹配双字节字符(包括汉字在内):[^\x00-\xff] * @param validateStr * @return */ public static int getRegExpLength(String validateStr){ // String temp = validateStr.replaceAll("[\u4e00-\u9fa5]", "**"); String temp = validateStr.replaceAll("[^\\x00-\\xff]", "**"); return temp.length(); } } </span>
相关文章推荐
- Java--计算中英文长度的若干种方法
- Java中英文混和字符串长度计算
- 利用PHP函数计算中英文字符串长度的方法
- java中计算字符串长度的方法
- 利用PHP函数计算中英文字符串长度的方法
- 利用PHP函数计算中英文字符串长度的方法
- 用Java实现按字节长度截取中英文数字字符串的方法总结
- iOS计算中英文混合字符串长度的方法
- Java中几种计算长度的方法
- java中计算字符串长度的方法及u4E00与u9FBB的认识
- 处理字符串的一些js/jq方法(去除HTML,去除空格,计算真实长度,截取中英文字符)
- 处理字符串的一些js/jq方法(去除HTML,去除空格,计算真实长度,截取中英文字符)
- JAVA怎么计算中英文合并的字符串的长度
- 用Java实现按字节长度截取中英文数字字符串的方法总结
- 用Java实现按字节长度截取中英文数字字符串的方法总结
- 计算中英文合并的字符串的长度
- 用java截取字符串长度的方法
- C#截取指定长度中英文字符串方法
- 用Java实现按字节长度截取字符串的方法
- 用Java实现按字节长度截取字符串的方法