Java斷定字符串中能否包括中文辦法。本站提示廣大學習愛好者:(Java斷定字符串中能否包括中文辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java斷定字符串中能否包括中文辦法正文
明天和同事在評論辯論一個成績,須要檢討“輸出的字符串中能否包括中文”,剛開端想到是用正則表達式,正則表達式中是以[u4e00-u9fa5]來全婚配字符能否是中文,但如今面對的成績是這個字符串中還能夠包括英文字符、數字、特別字符,一時也沒想出能婚配該場景的正則表達式,後來在網上搜了下,可使用Matcher類來處理該成績,年夜致的代碼完成以下:
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;
}