我有一个函数来检查一个字符串(大部分字符串只有一个 CJK 字符)是否只有单词字符,它会被调用很多次,所以成本是不可接受的,但我不知道如何优化一下,有什么建议吗?
/*\w is equivalent to the character class [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}].
For more details see Unicode TR-18, and bear in mind that the set of characters
in each class can vary between Unicode releases.*/
private static final Pattern sOnlyWordChars = Pattern.compile("\\w+");
private boolean isOnlyWordChars(String s) {
return sOnlyWordChars.matcher(s).matches();
}
当 s 为“3g”、“go_url”或“hao123”时,isOnlyWordChars(s) 应返回 true。