counter ASCII character as one, otherwise two - Android java.lang

Android examples for java.lang:Character

Description

counter ASCII character as one, otherwise two

Demo Code

import android.text.TextUtils;
import java.util.Locale;
import java.util.UUID;

public class Main{

    /**/*from  ww  w  . j a  v a 2s .  c om*/
     * counter ASCII character as one, otherwise two
     *
     * @param str
     * @return count
     */
    public static int counterChars(String str) {
        // return
        if (TextUtils.isEmpty(str)) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            int tmp = (int) str.charAt(i);
            if (tmp > 0 && tmp < 127) {
                count += 1;
            } else {
                count += 2;
            }
        }
        return count;
    }

}

Related Tutorials