一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java 判斷字符為中文實例代碼(超管用)

Java 判斷字符為中文實例代碼(超管用)

2020-03-26 14:12mrr JAVA教程

在做項目中經(jīng)常會遇到有項目需求是需要判斷字符為中文的一些問題,所以搜集了判斷中文字符的代碼片段,特此分享供大家參考

在做項目中經(jīng)常會遇到有項目需求是需要判斷字符為中文的一些問題,所以搜集了判斷中文字符的代碼片段,特此分享供大家參考。

直接貼出代碼了,里面有詳細的注釋。

?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.coder4j.main;
import java.util.regex.Pattern;
/**
* Java 判斷中文字符
*
* @author Chinaxiang
* @date 2015-08-11
*
*/
public class CheckChinese {
public static void main(String[] args) {
// 純英文
String s1 = "Hello,Tom.!@#$%^&*()_+-={}|[];':\"?";
// 純中文(不含中文標點)
String s2 = "你好中國";
// 純中文(含中文標點)
String s3 = "你好,中國。《》:“”‘';()【】!¥、";
// 韓文
String s4 = "????";
// 日文
String s5 = "ぎじゅつ";
// 特殊字符
String s6 = "??";
String s7 = "╃";
String s8 = "╂";
// 繁體中文
String s9 = "蒼老師";
// 1 使用字符范圍判斷
System.out.println("s1是否包含中文:" + hasChineseByRange(s1));// false
System.out.println("s2是否包含中文:" + hasChineseByRange(s2));// true
System.out.println("s3是否包含中文:" + hasChineseByRange(s3));// true
System.out.println("s4是否包含中文:" + hasChineseByRange(s4));// false
System.out.println("s5是否包含中文:" + hasChineseByRange(s5));// false
System.out.println("s6是否包含中文:" + hasChineseByRange(s6));// false
System.out.println("s7是否包含中文:" + hasChineseByRange(s7));// false
System.out.println("s8是否包含中文:" + hasChineseByRange(s8));// false
System.out.println("s9是否包含中文:" + hasChineseByRange(s9));// true
System.out.println("-------分割線-------");
System.out.println("s1是否全是中文:" + isChineseByRange(s1));// false
System.out.println("s2是否全是中文:" + isChineseByRange(s2));// true
System.out.println("s3是否全是中文:" + isChineseByRange(s3));// false 中文標點不在范圍內(nèi)
System.out.println("s4是否全是中文:" + isChineseByRange(s4));// false
System.out.println("s5是否全是中文:" + isChineseByRange(s5));// false
System.out.println("s6是否全是中文:" + isChineseByRange(s6));// false
System.out.println("s7是否全是中文:" + isChineseByRange(s7));// false
System.out.println("s8是否全是中文:" + isChineseByRange(s8));// false
System.out.println("s9是否全是中文:" + isChineseByRange(s9));// true
System.out.println("-------分割線-------");
// 2 使用字符范圍正則判斷(結(jié)果同1)
System.out.println("s1是否包含中文:" + hasChineseByReg(s1));// false
System.out.println("s2是否包含中文:" + hasChineseByReg(s2));// true
System.out.println("s3是否包含中文:" + hasChineseByReg(s3));// true
System.out.println("s4是否包含中文:" + hasChineseByReg(s4));// false
System.out.println("s5是否包含中文:" + hasChineseByReg(s5));// false
System.out.println("s6是否包含中文:" + hasChineseByReg(s6));// false
System.out.println("s7是否包含中文:" + hasChineseByReg(s7));// false
System.out.println("s8是否包含中文:" + hasChineseByReg(s8));// false
System.out.println("s9是否包含中文:" + hasChineseByReg(s9));// true
System.out.println("-------分割線-------");
System.out.println("s1是否全是中文:" + isChineseByReg(s1));// false
System.out.println("s2是否全是中文:" + isChineseByReg(s2));// true
System.out.println("s3是否全是中文:" + isChineseByReg(s3));// false 中文標點不在范圍內(nèi)
System.out.println("s4是否全是中文:" + isChineseByReg(s4));// false
System.out.println("s5是否全是中文:" + isChineseByReg(s5));// false
System.out.println("s6是否全是中文:" + isChineseByReg(s6));// false
System.out.println("s7是否全是中文:" + isChineseByReg(s7));// false
System.out.println("s8是否全是中文:" + isChineseByReg(s8));// false
System.out.println("s9是否全是中文:" + isChineseByReg(s9));// true
System.out.println("-------分割線-------");
// 3 使用CJK字符集判斷
System.out.println("s1是否包含中文:" + hasChinese(s1));// false
System.out.println("s2是否包含中文:" + hasChinese(s2));// true
System.out.println("s3是否包含中文:" + hasChinese(s3));// true
System.out.println("s4是否包含中文:" + hasChinese(s4));// false
System.out.println("s5是否包含中文:" + hasChinese(s5));// false
System.out.println("s6是否包含中文:" + hasChinese(s6));// false
System.out.println("s7是否包含中文:" + hasChinese(s7));// false
System.out.println("s8是否包含中文:" + hasChinese(s8));// false
System.out.println("s9是否包含中文:" + hasChinese(s9));// true
System.out.println("-------分割線-------");
System.out.println("s1是否全是中文:" + isChinese(s1));// false
System.out.println("s2是否全是中文:" + isChinese(s2));// true
System.out.println("s3是否全是中文:" + isChinese(s3));// true 中文標點也被包含進來
System.out.println("s4是否全是中文:" + isChinese(s4));// false
System.out.println("s5是否全是中文:" + isChinese(s5));// false
System.out.println("s6是否全是中文:" + isChinese(s6));// false
System.out.println("s7是否全是中文:" + isChinese(s7));// false
System.out.println("s8是否全是中文:" + isChinese(s8));// false
System.out.println("s9是否全是中文:" + isChinese(s9));// true
}
/**
* 是否包含中文字符<br>
* 包含中文標點符號<br>
*
* @param str
* @return
*/
public static boolean hasChinese(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (isChinese(c)) {
return true;
}
}
return false;
}
/**
* 是否全是中文字符<br>
* 包含中文標點符號<br>
*
* @param str
* @return
*/
public static boolean isChinese(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (!isChinese(c)) {
return false;
}
}
return true;
}
/**
* 是否是中文字符<br>
* 包含中文標點符號<br>
*
* @param c
* @return
*/
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D) {
return true;
} else if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
} else if (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 是否包含漢字<br>
* 根據(jù)漢字編碼范圍進行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,。《》()“‘'”、!¥等符號)<br>
*
* @param str
* @return
*/
public static boolean hasChineseByReg(String str) {
if (str == null) {
return false;
}
Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
return pattern.matcher(str).find();
}
/**
* 是否全是漢字<br>
* 根據(jù)漢字編碼范圍進行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,。《》()“‘'”、!¥等符號)<br>
*
* @param str
* @return
*/
public static boolean isChineseByReg(String str) {
if (str == null) {
return false;
}
Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
return pattern.matcher(str).matches();
}
/**
* 是否包含漢字<br>
* 根據(jù)漢字編碼范圍進行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,。《》()“‘'”、!¥等符號)<br>
*
* @param str
* @return
*/
public static boolean hasChineseByRange(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (c >= 0x4E00 && c <= 0x9FBF) {
return true;
}
}
return false;
}
/**
* 是否全是漢字<br>
* 根據(jù)漢字編碼范圍進行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,。《》()“‘'”、!¥等符號)<br>
*
* @param str
* @return
*/
public static boolean isChineseByRange(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (c < 0x4E00 || c > 0x9FBF) {
return false;
}
}
return true;
}
}

如果僅僅去判斷是否是中文,不需判斷中文標點的話,推薦使用正則去匹配,可能更高效點。

以上代碼內(nèi)容給大家介紹了Java 判斷字符為中文實例代碼(超管用),希望對大家有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 视频在线观看入口一二三2021 | 国产一级视频在线观看 | 暖暖视频免费观看视频中国.韩剧 | 国产精品日韩欧美一区二区 | 黑白配高清hd在线视频 | 国产视频在线一区 | 国产精品视频1区 | 男同桌脱我奶罩吸我奶作文 | 国产男人搡女人免费视频 | 大陆国产vs国产对白 | 91精品啪在线观看国产日本 | 四虎影院在线免费播放 | 亚洲男人网 | free嫩白的12sex性自由 | 日本免费在线观看 | 国产尤物精品视频 | 国产成人无精品久久久久国语 | 高h细节肉爽文办公室 | 亚洲精品国产一区二区三区在 | 美女的让男人桶爽30分钟的 | 魔兽官方小说 | 免费一级特黄特色大片∵黄 | 免费观看毛片视频 | 5555kkkk香蕉在线观看 | 四虎黄色影视库 | 国产伦码精品一区二区 | 国产欧美视频在线观看 | 成人无高清96免费 | 色戒完整版| 无码一区二区三区视频 | 亚洲好视频 | 青青热久免费精品视频网站 | 美女把腿开让我 | 小SAO货叫大声点妓女 | 91制片厂制作果冻传媒2021 | 免费高清视频日本 | 手机看片日韩1024你懂的首页 | 大陆黄色片 | 欧美日韩免费一区二区在线观看 | 亚洲卡一卡2卡三卡4卡无卡三 | 国产欧美日韩一区二区三区在线 |