java的中文_Java 判断中文字符

1 packagecom.coder4j.main;2

3 importjava.util.regex.Pattern;4

5 /**

6 * Java 判断中文字符7 *8 *@authorChinaxiang9 * @date 2015-08-1110 *11 */

12 public classCheckChinese {13

14 public static voidmain(String[] args) {15 //纯英文

16 String s1 = "Hello,Tom.!@#$%^&*()_+-={}|[];':\"?";17 //纯中文(不含中文标点)

18 String s2 = "你好中国";19 //纯中文(含中文标点)

20 String s3 = "你好,中国。《》:“”‘’;()【】!¥、";21 //韩文

22 String s4 = "한국어난";23 //日文

24 String s5 = "ぎじゅつ";25 //特殊字符

26 String s6 = "��";27 String s7 = "╃";28 String s8 = "╂";29 //繁体中文

30 String s9 = "蒼老師";31

32 //1 使用字符范围判断

33 System.out.println("s1是否包含中文:" + hasChineseByRange(s1));//false

34 System.out.println("s2是否包含中文:" + hasChineseByRange(s2));//true

35 System.out.println("s3是否包含中文:" + hasChineseByRange(s3));//true

36 System.out.println("s4是否包含中文:" + hasChineseByRange(s4));//false

37 System.out.println("s5是否包含中文:" + hasChineseByRange(s5));//false

38 System.out.println("s6是否包含中文:" + hasChineseByRange(s6));//false

39 System.out.println("s7是否包含中文:" + hasChineseByRange(s7));//false

40 System.out.println("s8是否包含中文:" + hasChineseByRange(s8));//false

41 System.out.println("s9是否包含中文:" + hasChineseByRange(s9));//true

42 System.out.println("-------分割线-------");43 System.out.println("s1是否全是中文:" + isChineseByRange(s1));//false

44 System.out.println("s2是否全是中文:" + isChineseByRange(s2));//true

45 System.out.println("s3是否全是中文:" + isChineseByRange(s3));//false 中文标点不在范围内

46 System.out.println("s4是否全是中文:" + isChineseByRange(s4));//false

47 System.out.println("s5是否全是中文:" + isChineseByRange(s5));//false

48 System.out.println("s6是否全是中文:" + isChineseByRange(s6));//false

49 System.out.println("s7是否全是中文:" + isChineseByRange(s7));//false

50 System.out.println("s8是否全是中文:" + isChineseByRange(s8));//false

51 System.out.println("s9是否全是中文:" + isChineseByRange(s9));//true

52 System.out.println("-------分割线-------");53 //2 使用字符范围正则判断(结果同1)

54 System.out.println("s1是否包含中文:" + hasChineseByReg(s1));//false

55 System.out.println("s2是否包含中文:" + hasChineseByReg(s2));//true

56 System.out.println("s3是否包含中文:" + hasChineseByReg(s3));//true

57 System.out.println("s4是否包含中文:" + hasChineseByReg(s4));//false

58 System.out.println("s5是否包含中文:" + hasChineseByReg(s5));//false

59 System.out.println("s6是否包含中文:" + hasChineseByReg(s6));//false

60 System.out.println("s7是否包含中文:" + hasChineseByReg(s7));//false

61 System.out.println("s8是否包含中文:" + hasChineseByReg(s8));//false

62 System.out.println("s9是否包含中文:" + hasChineseByReg(s9));//true

63 System.out.println("-------分割线-------");64 System.out.println("s1是否全是中文:" + isChineseByReg(s1));//false

65 System.out.println("s2是否全是中文:" + isChineseByReg(s2));//true

66 System.out.println("s3是否全是中文:" + isChineseByReg(s3));//false 中文标点不在范围内

67 System.out.println("s4是否全是中文:" + isChineseByReg(s4));//false

68 System.out.println("s5是否全是中文:" + isChineseByReg(s5));//false

69 System.out.println("s6是否全是中文:" + isChineseByReg(s6));//false

70 System.out.println("s7是否全是中文:" + isChineseByReg(s7));//false

71 System.out.println("s8是否全是中文:" + isChineseByReg(s8));//false

72 System.out.println("s9是否全是中文:" + isChineseByReg(s9));//true

73 System.out.println("-------分割线-------");74 //3 使用CJK字符集判断

75 System.out.println("s1是否包含中文:" + hasChinese(s1));//false

76 System.out.println("s2是否包含中文:" + hasChinese(s2));//true

77 System.out.println("s3是否包含中文:" + hasChinese(s3));//true

78 System.out.println("s4是否包含中文:" + hasChinese(s4));//false

79 System.out.println("s5是否包含中文:" + hasChinese(s5));//false

80 System.out.println("s6是否包含中文:" + hasChinese(s6));//false

81 System.out.println("s7是否包含中文:" + hasChinese(s7));//false

82 System.out.println("s8是否包含中文:" + hasChinese(s8));//false

83 System.out.println("s9是否包含中文:" + hasChinese(s9));//true

84 System.out.println("-------分割线-------");85 System.out.println("s1是否全是中文:" + isChinese(s1));//false

86 System.out.println("s2是否全是中文:" + isChinese(s2));//true

87 System.out.println("s3是否全是中文:" + isChinese(s3));//true 中文标点也被包含进来

88 System.out.println("s4是否全是中文:" + isChinese(s4));//false

89 System.out.println("s5是否全是中文:" + isChinese(s5));//false

90 System.out.println("s6是否全是中文:" + isChinese(s6));//false

91 System.out.println("s7是否全是中文:" + isChinese(s7));//false

92 System.out.println("s8是否全是中文:" + isChinese(s8));//false

93 System.out.println("s9是否全是中文:" + isChinese(s9));//true

94

95 }96

97 /**

98 * 是否包含中文字符
99 * 包含中文标点符号
100 *101 *@paramstr102 *@return

103 */

104 public static booleanhasChinese(String str) {105 if (str == null) {106 return false;107 }108 char[] ch =str.toCharArray();109 for (charc : ch) {110 if(isChinese(c)) {111 return true;112 }113 }114 return false;115 }116

117 /**

118 * 是否全是中文字符
119 * 包含中文标点符号
120 *121 *@paramstr122 *@return

123 */

124 public static booleanisChinese(String str) {125 if (str == null) {126 return false;127 }128 char[] ch =str.toCharArray();129 for (charc : ch) {130 if (!isChinese(c)) {131 return false;132 }133 }134 return true;135 }136

137 /**

138 * 是否是中文字符
139 * 包含中文标点符号
140 *141 *@paramc142 *@return

143 */

144 private static boolean isChinese(charc) {145 Character.UnicodeBlock ub =Character.UnicodeBlock.of(c);146 if (ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {147 return true;148 } else if (ub ==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) {149 return true;150 } else if (ub ==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {151 return true;152 } else if (ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {153 return true;154 } else if (ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) {155 return true;156 } else if (ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C) {157 return true;158 } else if (ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D) {159 return true;160 } else if (ub ==Character.UnicodeBlock.GENERAL_PUNCTUATION) {161 return true;162 } else if (ub ==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {163 return true;164 }165 return false;166 }167

168 /**

169 * 是否包含汉字
170 * 根据汉字编码范围进行判断
171 * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)
172 *173 *@paramstr174 *@return

175 */

176 public static booleanhasChineseByReg(String str) {177 if (str == null) {178 return false;179 }180 Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");181 returnpattern.matcher(str).find();182 }183

184 /**

185 * 是否全是汉字
186 * 根据汉字编码范围进行判断
187 * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)
188 *189 *@paramstr190 *@return

191 */

192 public static booleanisChineseByReg(String str) {193 if (str == null) {194 return false;195 }196 Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");197 returnpattern.matcher(str).matches();198 }199

200 /**

201 * 是否包含汉字
202 * 根据汉字编码范围进行判断
203 * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)
204 *205 *@paramstr206 *@return

207 */

208 public static booleanhasChineseByRange(String str) {209 if (str == null) {210 return false;211 }212 char[] ch =str.toCharArray();213 for (charc : ch) {214 if (c >= 0x4E00 && c <= 0x9FBF) {215 return true;216 }217 }218 return false;219 }220

221 /**

222 * 是否全是汉字
223 * 根据汉字编码范围进行判断
224 * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)
225 *226 *@paramstr227 *@return

228 */

229 public static booleanisChineseByRange(String str) {230 if (str == null) {231 return false;232 }233 char[] ch =str.toCharArray();234 for (charc : ch) {235 if (c < 0x4E00 || c > 0x9FBF) {236 return false;237 }238 }239 return true;240 }241

242 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值