How to find the first Chinese character in a java string example:
String str = "xing 杨某某";
How can i get the index of first Chinese character 杨 in above str. Thanks!
This could help:
public static int firstChineseChar(String s) {
for (int i = 0; i < s.length(); ) {
int index = i;
int codepoint = s.codePointAt(i);
i += Character.charCount(codepoint);
if (Character.UnicodeScript.of(codepoint) == Character.UnicodeScript.HAN) {
return index;
}
}
return -1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With