Check the validity of the Length of a UTF8 String. - Java java.lang

Java examples for java.lang:String UTF

Description

Check the validity of the Length of a UTF8 String.

Demo Code


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

public class Main {
    /**//w  w w . java 2 s. c  o m
     * Check the validity of the Length of a UTF8 String.
     *
     * @param min
     * @param max
     * @param string
     * @return boolean indicating UTF-8 String Length is valid or not.
     */
    public static boolean isUTF8StringLengthValid(int min, int max,
            String string) {
        if (string == null) {
            return false;
        }
        try {
            byte[] bytes = string.getBytes("UTF-8");
            return ((bytes.length >= min) && (bytes.length <= max));
        } catch (UnsupportedEncodingException uee) {
            // Ignore...
        }
        return false;
    }
}

Related Tutorials