Java String get Chinese String length

Description

Java String get Chinese String length


import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

    public static int getGBCount(String str) {
        int count = 0;
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            for (int i = 0; i <= m.groupCount(); i++) {
                count = count + 1;
            }
        }
        return count;
    }
}



PreviousNext

Related