java中判断字段真实长度(中文2个字符,英文1个字符)的方法 http://blog.sina.com.cn/s/blog_4a4f9fb50100s9dl.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Char_cn { public static void main(String[] args) { // TODO Auto-generated method stub String haha = "我叫兜兜abcd"; int true_num = String_length(haha); System.out.println("true" + true_num); int false_num = haha.length(); System.out.print("flase" + false_num); } public static int String_length(String value) { int valueLength = 0; String chinese = "[\u4e00-\u9fa5]"; for (int i = 0; i < value.length(); i++) { String temp = value.substring(i, i + 1); if (temp.matches(chinese)) { valueLength += 2; } else { valueLength += 1; } } return valueLength; } } |
1、判断字符串是否为连续的中文字符(不包含英文及其他任何符号和数字):
Regex.IsMatch(“中文”,”^[/u4e00-/u9fa5]”);
2、判断字符串是否为中文字符串(仅不包含英文但可以包含其他符号及数字):
!Regex.IsMatch(“中文”,@”[a-zA-Z]”);
Java判断字符串是否包含中文以及包含的中文个数 http://www.voidcn.com/blog/baidu_18607183/article/p-2250732.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternTest { public static void main(String[] args) { String str = "你好啊世界! hello world!/にほんご"; int chinaCount = 0; Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]"); int length = 0; if (str != null) { Matcher aMatcher = pattern.matcher(str); System.out.println("是否有中文:" + (aMatcher.find() ? "有" : "无")); char c[] = str.toCharArray(); length = c.length; for (int i = 0; i < length; i++) { Matcher matcher = pattern.matcher(String.valueOf(c[i])); if (matcher.matches()) { chinaCount++; } } } System.out.println("字符串总个数:" + length); System.out.println("其中中文个数:" + chinaCount); System.out.println("非中文个数:" + (length - chinaCount)); } } |