get Chinese Char Length - Android Internationalization

Android examples for Internationalization:Chinese

Description

get Chinese Char Length

Demo Code

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

public class Main {

  private static String chineseEx = "[\\u4e00-\\u9fa5]";

  public static float getChineseCharLength(String value) {
    float count = 0;
    Pattern p = Pattern.compile(chineseEx);
    Matcher m = p.matcher(value);
    while (m.find()) {
      for (int i = 0; i <= m.groupCount(); i++) {
        count++;/*  www  .  java 2s  . co m*/
      }
    }
    count += 0.5f * (value.length() - count);
    return count;
  }

}

Related Tutorials