java提取字符串的汉字
2018-03-25 21:23:05
824 次阅读
0 个评论
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChineseUtil {
private static String regEx = "[\u4e00-\u9fa5]+";
public static void main(String[] args) {
String str = "abc123ABC好评123我是汉字";
if(checkChineseCharacter(str))
{
System.out.println(returnChineseCharacter(str));
}
}
public static boolean checkChineseCharacter(String str)
{
if(str.getBytes().length == str.length())
return false;
return true;
}
public static String returnChineseCharacter(String str)
{
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
String chiResult = "";
while (m.find()) {
chiResult += m.group();
}
return chiResult;
}
}
00