Get char Length by counting Chinese character as two - Android Internationalization

Android examples for Internationalization:Chinese

Description

Get char Length by counting Chinese character as two

Demo Code

import android.text.TextUtils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static int charLength(String str) {
        int size = str.length();
        int len = 0;
        for (int i = 0; i < size; i++) {
            char c = str.charAt(i);
            if ((c >= 0x4e00) && (c <= 0x9fbb)) {
                len += 2;/*from w  ww .j a va2  s  . co  m*/
            } else {
                len += 1;
            }
        }
        return len;
    }

}

Related Tutorials