get String Length for unicode string - Android java.lang

Android examples for java.lang:String Unicode

Description

get String Length for unicode string

Demo Code


public class Main{

    public static int getStringLength(String s) {
        char[] chs = s.toCharArray();
        int count = 0;
        for (int i = 0; i < chs.length; i++) {
            if ((chs[i] + "").getBytes().length >= 2) {
                count += 2;/*from w  w w  .j a  va  2 s .  c o m*/
            } else {
                count++;
            }
        }
        return count;
    }

}

Related Tutorials