Determines if the String has any UTF-8 Type Characters in it's Data. - Java java.lang

Java examples for java.lang:String UTF

Description

Determines if the String has any UTF-8 Type Characters in it's Data.

Demo Code


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

public class Main {
    /**/*  w w  w  .ja  va2s  .  c o m*/
     * Determines if the String has any UTF-8 Type Characters in it's Data.
     *
     * @param string
     * @return boolean
     */
    public static boolean isStringUTF8(String string) {
        if (string == null) {
            return false;
        }
        return isUTF8StringLengthValid(0, string.length(), string);
    }

    /**
     * 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