使用正则判断一个字符串中是否包含中文或者中文字符

 

代码实现如下:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Miracle Luna on 2019/12/20
 */
public class ChineseCheck {

    public static void main(String[] args) {
        String str = "Hello! 《满江红》";
        System.out.println("==> " + isContainChinese(str));
    }

    /**
     * 字符串是否包含中文
     * @param str 待校验字符串
     * @return true 包含中文字符 false 不包含中文字符
     */
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

}

 

执行结果如下:

==> true

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-08-31
猜你喜欢
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2022-02-11
相关资源
相似解决方案