Java String length for Chinese

Description

Java String length for Chinese


public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "";
        System.out.println(chineseLength(str));
    }//from   w  ww . j a va  2s  .  co m

    public static int chineseLength(String str) {
        int valueLength = 0;
        String chinese = "[\u0391-\uFFE5]";
        if (!isEmpty(str)) {
            for (int i = 0; i < str.length(); i++) {
                String temp = str.substring(i, i + 1);
                if (temp.matches(chinese)) {
                    valueLength += 2;
                }
            }
        }
        return valueLength;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0 || "null".equals(str.trim());
    }
}



PreviousNext

Related