Java String to Boolean asBoolean(String value)

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

Description

as Boolean

License

Open Source License

Declaration

public static boolean asBoolean(String value) 

Method Source Code

//package com.java2s;
/**//from w  w w.j  a v  a2s. c  o  m
 *  Copyright (c) 2013-2014 Angelo ZERR.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */

public class Main {
    public static final String TRUE = "true";
    public static final String FALSE = "false";
    public static final String YES = "yes";
    public static final String NO = "no";

    public static boolean asBoolean(String value) {
        return asBoolean(value, false);
    }

    public static boolean asBoolean(String value, boolean defaultValue) {
        if (value == null)
            return defaultValue;
        value = value.trim();
        if (defaultValue)
            return !(FALSE.equals(value.toLowerCase()) || NO.equals(value.toLowerCase()));
        return TRUE.equals(value.toLowerCase()) || YES.equals(value.toLowerCase());
    }
}

Related

  1. asBoolean(String flag)
  2. asBoolean(String name, boolean value)
  3. asBoolean(String s)
  4. asBoolean(String str)
  5. asBoolean(String string)
  6. asBoolean(String value, boolean defaultValue)
  7. atob(final String str, final boolean def)
  8. atob(String pString_)
  9. atob(String s)