Java判斷字符串中是否包含中文方法
今天和同事在討論一個(gè)問(wèn)題,需要檢查“輸入的字符串中是否包含中文”,剛開始想到是用正則表達(dá)式,正則表達(dá)式中是以[u4e00-u9fa5]來(lái)全匹配字符是否是中文,但現(xiàn)在面臨的問(wèn)題是這個(gè)字符串中還可能包含英文字符、數(shù)字、特殊字符,一時(shí)也沒(méi)想出能匹配該場(chǎng)景的正則表達(dá)式,后來(lái)在網(wǎng)上搜了下,可以使用Matcher類來(lái)解決該問(wèn)題,大致的代碼實(shí)現(xiàn)如下:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class demo { static String regEx = "[\u4e00-\u9fa5]"; static Pattern pat = Pattern.compile(regEx); public static void main(String[] args) { String input = "Hell world!"; System.out.println(isContainsChinese(input)); input = "hello world"; System.out.println(isContainsChinese(input)); } public static boolean isContainsChinese(String str) { Matcher matcher = pat.matcher(str); boolean flg = false; if (matcher.find()) { flg = true; } return flg; }
相關(guān)文章
IDEA未配置SQL方言:無(wú)法使用SQL提示解決方法
在使用IDEA進(jìn)行SQL開發(fā)時(shí),如果未配置SQL方言可能會(huì)導(dǎo)致一些問(wèn)題,如無(wú)法正確識(shí)別數(shù)據(jù)庫(kù)中的關(guān)鍵字、數(shù)據(jù)類型等,這篇文章主要給大家介紹了關(guān)于IDEA未配置SQL方言,無(wú)法使用SQL提示解決方法的相關(guān)資料,需要的朋友可以參考下2024-07-07

舉例講解Java編程中this關(guān)鍵字與super關(guān)鍵字的用法

IDEA整合SSM框架實(shí)現(xiàn)網(wǎng)頁(yè)上顯示數(shù)據(jù)

簡(jiǎn)單了解Spring Web相關(guān)模塊運(yùn)行原理