去除字符串中的中文
核心:找出中文的正则表达式,将中文替换为空字符串 “”。
方法一、直接使用String类自带的replaceAll()替换
private static String REGEX_CHINESE = "[\u4e00-\u9fa5]";
@Test
public void test(){
String str = "3A都闻到";
String s = str.replaceAll(REGEX_CHINESE, "");
System.out.println(s); // 输出3A
}
方法二、使用java.util.regex下Pattern类中的方法
private static String REGEX_CHINESE = "[\u4e00-\u9fa5]";
@Test
public void test(){
String str = "3A都闻到";
Matcher matcher = Pattern.compile(REGEX_CHINESE).matcher(str);
String s = matcher.replaceAll("");
System.out.println(s); // 输出3A
}