我有一個函數來檢查一個字符串(大部分的字符串是否只有一個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。
你認爲空字符串有效嗎? (即w *而不是w +) – SK9
可能的重複[最快的方法來檢查字符串是否只包含數字](http://stackoverflow.com/questions/7461080/fastest-way-to-check-if-string-contains-只有數字) – SK9
@ SK9 c#和java是不同的語言:P – nachokk