get Chars Length for unicode string using regular expression - Android java.lang

Android examples for java.lang:String Unicode

Description

get Chars Length for unicode string using regular expression

Demo Code

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

public class Main{

    public static int getCharsLength(String str) {
        int length = 0;
        try {/*from   w  w  w.j av a 2s  . co m*/
            if (str == null) {
                return 0;
            }
            str = str.replaceAll("[^\\x00-\\xff]", "**");
            length += str.length();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

}

Related Tutorials