Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS : 4E00-9FBF:CJK 统一表意符号
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS :F900-FAFF:CJK 兼容象形文字 Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A :3400-4DBF:CJK 统一表意符号扩展 A
CJK的意思是“Chinese,Japanese,Korea”的简写 ,实际上就是指中日韩三国的象形文字的Unicode编码
Character.UnicodeBlock.GENERAL_PUNCTUATION :2000-206F:常用标点 Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION :3000-303F:CJK 符号和标点 Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS :FF00-FFEF:半角及全角形式
复制代码
1 /**
2 * 校验一个字符是否是汉字
3 *
4 * @param c
5 * 被校验的字符
6 * @return true代表是汉字
7 */
8 public static boolean isChineseChar(char c) {
9 try {
10 return String.valueOf(c).getBytes(“UTF-8”).length > 1;
11 } catch (UnsupportedEncodingException e) {
12 e.printStackTrace();
13 return false;
14 }
15 }
16
17 /**
18 * 验证字符串内容是否包含下列非法字符
19 * ~!#%^&*=+\|{};:'\",<>/?○●★☆☉♀♂※¤╬の〆
~!#%^&*=+\|{};:’\”,<>/?○●★☆☉♀♂※¤╬の〆”;
20 *
21 * @param content
22 * 字符串内容
23 * @return 't'代表不包含非法字符,otherwise代表包含非法字符。
24 */
25 public static char validateLegalString(String content) {
26 String illegal = "
27 char isLegalChar = ‘t’;
28 L1: for (int i = 0; i < content.length(); i++) {
29 for (int j = 0; j < illegal.length(); j++) {
30 if (content.charAt(i) == illegal.charAt(j)) {
31 isLegalChar = content.charAt(i);
32 break L1;
33 }
34 }
35 }
36 return isLegalChar;
37 }
38
39 /**
40 * 验证是否是汉字或者0-9、a-z、A-Z
41 *
42 * @param c
43 * 被验证的char
44 * @return true代表符合条件
45 */
46 public static boolean isRightChar(char c) {
47 return isChinese(c) || isWord(c);
48 }
49
50 /**
51 * 校验某个字符是否是a-z、A-Z、_、0-9
52 *
53 * @param c
54 * 被校验的字符
55 * @return true代表符合条件
56 */
57 public static boolean isWord(char c) {
58 String regEx = “[\w]”;
59 Pattern p = Pattern.compile(regEx);
60 Matcher m = p.matcher(“” + c);
61 return m.matches();
62 }
63
64 /**
65 * 判定输入的是否是汉字
66 *
67 * @param c
68 * 被校验的字符
69 * @return true代表是汉字
70 */
71 public static boolean isChinese(char c) {
72 Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
73 if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
74 || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
75 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
76 || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
77 || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
78 || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
79 return true;
80 }
81 return false;
82 }
83
84 /**
85 * 校验String是否全是中文
86 *
87 * @param name
88 * 被校验的字符串
89 * @return true代表全是汉字
90 */
91 public static boolean checkNameChese(String name) {
92 boolean res = true;
93 char[] cTemp = name.toCharArray();
94 for (int i = 0; i < name.length(); i++) {
95 if (!isChinese(cTemp[i])) {
96 res = false;
97 break;
98 }
99 }
100 return res;
101 }