Friday, June 6, 2014

Cách đếm số ký tự UTF-8 trong JAVA

Cách đếm số ký tự UTF-8 trong JAVA

/**
 *
 * @author HuanLT
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here+
        String g = "lê";
       
        System.out.println("" + countUTF8Length(g));
    }


    // count utf-8
    public static int countUTF8Length(String str)
    {
        int count = 0;
        for (int i = 0; i < str.length(); ++i)
        {
            char c = str.charAt(i);
            if (c < 0x80)
            {
                count++;
            } else if (c < 0x800)
            {
                count +=2;
            } else if (c < 0x8000)
            {
                count +=3;
            } else {
                throw new UnsupportedOperationException("not implemented yet");
            }
        }

        return count;
    }
}

No comments:

Post a Comment