get String Length for Chinese - Android Internationalization

Android examples for Internationalization:Chinese

Description

get String Length for Chinese

Demo Code


public class Main{

    public static int getStringLength(String string) {
        int valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < string.length(); i++) {
            String temp = string.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;/*from   ww w  . j  ava 2  s . c om*/
            } else {
                valueLength += 1;
            }
        }
        return valueLength;
    }

}

Related Tutorials