get string Length for mixed ascii and unicode - Android java.lang

Android examples for java.lang:String Unicode

Description

get string Length for mixed ascii and unicode

Demo Code

import android.text.TextUtils;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main{

    public static int getRealLength(String str) {
        if (str == null) {
            return 0;
        }/*from ww w . j  av a2 s  . c  om*/

        char separator = 256;
        int realLength = 0;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= separator) {
                realLength += 2;
            } else {
                realLength++;
            }
        }
        return realLength;
    }

}

Related Tutorials