Java Boolean From toBoolean(final String value)

Here you can find the source of toBoolean(final String value)

Description

To boolean.

License

Open Source License

Parameter

Parameter Description
value the value

Return

true, if successful

Declaration

public static boolean toBoolean(final String value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** The Constant DEFAULT_VALUE. */
    public static final boolean DEFAULT_VALUE = false;

    /**/*  w ww.  j a v  a  2 s .  co m*/
     * To boolean.
     *
     * @param value
     *            the value
     * @return true, if successful
     */
    public static boolean toBoolean(final String value) {
        return toBoolean(value, DEFAULT_VALUE);
    }

    /**
     * To boolean.
     *
     * @param value
     *            the value
     * @param defaultValue
     *            the default value
     * @return true, if successful
     */
    public static boolean toBoolean(final String value,
            final boolean defaultValue) {
        if (value == null || value.length() == 0) {
            return defaultValue;
        } else {
            if ("true".equalsIgnoreCase(value)) {
                return true;
            }
            if ("on".equalsIgnoreCase(value)) {
                return true;
            }
            if ("yes".equalsIgnoreCase(value)) {
                return true;
            }
            //
            if ("false".equalsIgnoreCase(value)) {
                return false;
            }
            if ("off".equalsIgnoreCase(value)) {
                return false;
            }
            if ("no".equalsIgnoreCase(value)) {
                return false;
            }
            //
            switch (value.charAt(0)) {
            case '1':
            case 't':
            case 'T':
            case 'y':
            case 'Y':
                return true;
            case '0':
            case 'f':
            case 'F':
            case 'n':
            case 'N':
                return false;
            default:
                return defaultValue;
            }
        }
    }
}

Related

  1. toBoolean(final String source)
  2. toBoolean(final String string)
  3. toBoolean(final String strTransparent)
  4. toBoolean(final String val)
  5. toBoolean(final String value)
  6. toBoolean(final String value)
  7. toBoolean(final String value, final boolean def)
  8. toBoolean(int i)
  9. toBoolean(int value)