Get length of UTF8 String Length - Java java.lang

Java examples for java.lang:String UTF

Description

Get length of UTF8 String Length

Demo Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*  w ww  . jav a2 s  .  c  om*/
     * Get length of UTF8 String Length
     *
     * @param string
     * @return int of UTF-8 String Length.
     */
    public static int getUTF8StringLength(String string) {
        if (string == null) {
            return 0;
        }
        try {
            return string.getBytes("UTF-8").length;
        } catch (UnsupportedEncodingException uee) {
            // Ignore...
        }
        return 0;
    }
}

Related Tutorials