returns true if the string parameter is any of the following values: "yes", "true", "on", "1". - Java java.lang

Java examples for java.lang:boolean

Description

returns true if the string parameter is any of the following values: "yes", "true", "on", "1".

Demo Code



public class Main{
    public static void main(String[] argv) throws Exception{
        String value = "java2s.com";
        System.out.println(isValueTrue(value));
    }//from  w w w .jav a2  s .com
    /** Constant <code>VALID_TRUE_VALUES="new String[] {yes, true, on, 1, y, t}"</code> */
    public static final String[] VALID_TRUE_VALUES = new String[] { "yes",
            "true", "on", "1", "y", "t" };
    /**
     * this method returns true if the string parameter is any of the following values:
     * "yes", "true", "on", "1". It will return false if the string is null or not one of the previous values.
     *
     * @param value The string we're trying to identify as a true boolean value.
     * @return true if the value represents one of the above values. False if not or null.
     */
    public static boolean isValueTrue(String value) {
        if (StringUtils.isEmpty(value))
            return false;
        boolean found = false;
        for (int i = 0; i < VALID_TRUE_VALUES.length && !found; i++) {
            found = VALID_TRUE_VALUES[i].equalsIgnoreCase(value.trim());
        }
        return found;
    }
}

Related Tutorials