get string Length By Byte - Android java.lang

Android examples for java.lang:String Algorithm

Description

get string Length By Byte

Demo Code

import android.text.Html;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static int getLengthByByte(String str) {
        int length = 0;
        if (str == null || str.length() == 0) {
            return length;
        }//  www  . j a v  a 2 s  .  c  om

        for (int i = 0; i < str.length(); i++) {
            int ascii = Character.codePointAt(str, i);
            if (ascii >= 0 && ascii <= 255) {
                length++;
            } else {
                length += 2;
            }
        }
        return length;
    }

}

Related Tutorials