前言
在日常開發(fā)編程中,我們有時(shí)從用戶那里得到一些輸入信息,對于特定應(yīng)用,部分信息不允許包含中文字符,那如何檢測信息字符串中是否包含中文字符呢? 方法有很多,這篇文章就介紹一下如何通過正則表達(dá)式來實(shí)現(xiàn)這個(gè)需求。
示例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package cn.sunzn.demo; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { System.out.println(isContainChinese( "中國China" )); } public static boolean isContainChinese(String str) { Pattern p = Pattern.compile( "[\u4e00-\u9fa5]" ); Matcher m = p.matcher(str); if (m.find()) { return true ; } return false ; } } |
運(yùn)行結(jié)果如下:
1
|
true |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。