Java String to Boolean stringToBoolean(String expr)

Here you can find the source of stringToBoolean(String expr)

Description

Converts a string to a boolean value, accepting "true", "t", "1", "yes" and "y" as true, accepting "false", "f", "0", "no", "n" as false, and throwing an IllegalArgumentException when none of these strings match.

License

Apache License

Parameter

Parameter Description
expr The boolean string

Return

The boolean value

Declaration

public static boolean stringToBoolean(String expr) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * //w  w w . ja  va  2  s.  c om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Converts a string to a boolean value, accepting "true", "t", "1", "yes"
     * and "y" as true, accepting "false", "f", "0", "no", "n" as false, and
     * throwing an IllegalArgumentException when none of these strings match.
     * The method's test is case-insensitive.
     * 
     * @param expr
     *            The boolean string
     * @return The boolean value
     */
    public static boolean stringToBoolean(String expr) {

        String cleanExpr = expr.toLowerCase().trim();

        if (cleanExpr.equals("true") || cleanExpr.equals("t") || cleanExpr.equals("1") || cleanExpr.equals("yes")
                || cleanExpr.equals("y")) {
            return true;
        } else if (cleanExpr.equals("false") || cleanExpr.equals("f") || cleanExpr.equals("0")
                || cleanExpr.equals("no") || cleanExpr.equals("n")) {
            return false;
        } else {
            throw new IllegalArgumentException("Expression could not be interpreted as boolean: " + expr);
        }
    }

    /**
     * Creates a new list that contains the same elements than the parameter
     * list, but with all string elements converted to lower case
     * 
     * @param listOriginal
     * @return The list with all strings converted to lower case
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List toLowerCase(List listOriginal) {

        List list = new ArrayList();
        Object elem;
        for (int i = 0; i < listOriginal.size(); i++) {
            elem = listOriginal.get(i);
            if (elem instanceof String) {
                list.add(((String) elem).toLowerCase());
            } else {
                list.add(elem);
            }
        }

        return list;

    }
}

Related

  1. atob(String s)
  2. convertStringToBoolean(String pValue)
  3. convertStringToBoolean(String strBoolean)
  4. convertStringToBoolean(String string)
  5. convertStringToBoolean(String string)
  6. stringToBooleanArray(String string, String delim)