Java Boolean From toBoolean(String value)

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

Description

to Boolean

License

Open Source License

Declaration

public static boolean toBoolean(String value) 

Method Source Code

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

public class Main {
    public static final String[] TRUE_VALUES = { "1", "t", "true", "y", "yes", "on", "checked" };
    public static final String[] FALSE_VALUES = { "0", "f", "false", "n", "no", "off", "unchecked" };

    public static boolean toBoolean(String value) {
        return toBoolean(value, false);
    }/*from  w  w  w.ja  va  2s.c  o  m*/

    public static boolean toBoolean(String value, boolean defaultValue) {

        if (value == null) {
            return defaultValue;
        } else {

            if (isTrueToken(value)) {
                return true;
            } else if (isFalseToken(value)) {
                return false;
            } else {
                return defaultValue;
            }
        }
    }

    public static boolean isTrueToken(String value) {

        boolean flag = false;

        if (value != null) {
            String trimmedValue = value.trim();
            flag = (trimmedValue.equalsIgnoreCase(TRUE_VALUES[0]) || trimmedValue.equalsIgnoreCase(TRUE_VALUES[1])
                    || trimmedValue.equalsIgnoreCase(TRUE_VALUES[2])
                    || trimmedValue.equalsIgnoreCase(TRUE_VALUES[3])
                    || trimmedValue.equalsIgnoreCase(TRUE_VALUES[3])
                    || trimmedValue.equalsIgnoreCase(TRUE_VALUES[4])
                    || trimmedValue.equalsIgnoreCase(TRUE_VALUES[5])
                    || trimmedValue.equalsIgnoreCase(TRUE_VALUES[6]));
        }

        return flag;
    }

    public static boolean isFalseToken(String value) {
        boolean flag = false;

        if (value != null) {
            String trimmedValue = value.trim();
            flag = (trimmedValue.equalsIgnoreCase(FALSE_VALUES[0]) || trimmedValue.equalsIgnoreCase(FALSE_VALUES[1])
                    || trimmedValue.equalsIgnoreCase(FALSE_VALUES[2])
                    || trimmedValue.equalsIgnoreCase(FALSE_VALUES[3])
                    || trimmedValue.equalsIgnoreCase(FALSE_VALUES[3])
                    || trimmedValue.equalsIgnoreCase(FALSE_VALUES[4])
                    || trimmedValue.equalsIgnoreCase(FALSE_VALUES[5])
                    || trimmedValue.equalsIgnoreCase(FALSE_VALUES[6]));
        }

        return flag;
    }
}

Related

  1. toBoolean(String string)
  2. toBoolean(String string)
  3. toBoolean(String string, boolean defaultValue)
  4. toBoolean(String text)
  5. toBoolean(String val)
  6. toBoolean(String value)
  7. toBoolean(String value)
  8. toBoolean(String value)
  9. toBoolean(String value)