检测字符串是否包含中文

Myth丶恋晨 2022-05-14 02:18 358阅读 0赞
  1. import org.apache.commons.lang3.StringUtils;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /** * 字符串是否包含中文 * * @param str 待校验字符串 * @return true 包含中文字符||字符串为空 false 不包含中文字符 */
  5. public static boolean isContainChinese(String str) {
  6. if (StringUtils.isEmpty(str)) {
  7. return true;
  8. }
  9. Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
  10. Matcher m = p.matcher(str);
  11. if (m.find()) {
  12. return true;
  13. }
  14. return false;
  15. }

测试:

  1. System.out.println(InfoUtil.isContainChinese(""));//true
  2. System.out.println(InfoUtil.isContainChinese("abc我"));//true
  3. System.out.println(InfoUtil.isContainChinese("abc123"));//false
  4. System.out.println(InfoUtil.isContainChinese("abc"));//false

发表评论

表情:
评论列表 (有 0 条评论,358人围观)

还没有评论,来说两句吧...

相关阅读

    相关 字符串是否包含问题

    题目描述 假设这有两个分别由字母组成的字符串A另外字符串B,字符串B的字母数较字符串A少一些。什么方法能最快地查出字符串B所有字母是不是都在字符串A里?也就是说判断字符串