Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the first Chinese character in a java string

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!

like image 516
star Avatar asked Dec 15 '22 06:12

star


1 Answers

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;
}
like image 181
Andrea Avatar answered Dec 17 '22 01:12

Andrea