Java - Write code to Convert a string value to a boolean.

Requirements

Write code to Convert a string value to a boolean.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "y";
        System.out.println(convertStrToBoolean(string));
    }/*ww w . java  2  s  . c  o  m*/

    /**
     * Convert a string value to a boolean.  If the value is a common positive string
     * value such as "1", "true", "y" or "yes" then TRUE is returned.  For any other
     * value or null, FALSE is returned.
     *
     * @param string value to test
     * @return true if the string resolves to a positive value.
     */
    public static boolean convertStrToBoolean(final String string) {
        return !(string == null || string.length() < 1)
                && (string.equalsIgnoreCase("true")
                        || string.equalsIgnoreCase("1")
                        || string.equalsIgnoreCase("yes") || string
                            .equalsIgnoreCase("y"));
    }
}