直接上代码,用正则表达式判断。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class isChinese {
public static void main(String[] args) {
System.out.println(isContainChinese("China"));
}
public static boolean isContainChinese(String str) {
//Pattern p = Pattern.compile("[\u4e00-\u9fa5]");//中文字符的正则表达
Pattern p = Pattern.compile("^[a-zA-Z]*"); //英文字符的正则表达
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
}
本文介绍了一个使用Java正则表达式判断字符串中是否包含中文或英文的方法,通过实例展示了如何实现这一功能。
1660

被折叠的 条评论
为什么被折叠?



