前言
在日常開發編程中,我們有時從用戶那里得到一些輸入信息,對于特定應用,部分信息不允許包含中文字符,那如何檢測信息字符串中是否包含中文字符呢? 方法有很多,這篇文章就介紹一下如何通過正則表達式來實現這個需求。
示例代碼
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 ; } } |
運行結果如下:
1
|
true |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。