get Real Length string length in GBK encoding - Java java.lang

Java examples for java.lang:String Length

Description

get Real Length string length in GBK encoding

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(getRealLength(str));
    }/*from  www  . j a va  2 s.c o m*/

    public static int getRealLength(String str) {
        try {
            return str.getBytes("GBK").length;
        } catch (UnsupportedEncodingException e) {
            return -1;
        }
    }
}

Related Tutorials