Get string length by charset encoding - Android java.lang

Android examples for java.lang:String

Description

Get string length by charset encoding

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/*  ww  w  .j av a2 s  . c o  m*/
     * ?????????????.
     * 
     * @param str
     *            ??
     * @param charset
     *            ????GBK?
     * @return the int
     */
    public static int strlen(String str, String charset) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        int length = 0;
        try {
            length = str.getBytes(charset).length;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

}

Related Tutorials