如何将String对象转换为Boolean对象?

Sur*_*nti 315 java string boolean

如何将String对象转换为Boolean对象?

KLE*_*KLE 503

尝试(取决于您想要的结果类型):

Boolean boolean1 = Boolean.valueOf("true");
boolean boolean2 = Boolean.parseBoolean("true");
Run Code Online (Sandbox Code Playgroud)

优点:

  • Boolean:这不会创建Boolean的新实例,因此性能更好(并且垃圾收集更少).它重用了两个Boolean.TRUE或两个实例Boolean.FALSE.
  • boolean:不需要实例,使用基本类型.

官方文档在Javadoc中.


更新:

也可以使用自动装箱,但它具有性能成本.
我建议只在你必须施展自己时才使用它,而不是在施法是可以避免的时候使用它.

  • 最大的问题是布尔在看到它不应该接受的东西时不会例外.对于它看作"true"的任何内容,它将返回true,并且对于其他所有内容*将返回false.如果您尝试将字符串强制匹配到适当的布尔值,则必须添加额外的逻辑来手动捕获非法案例. (8认同)
  • 如果 String 对象为 null 则 Boolean.valueOf(String) 将返回 false 。但是 Boolean 也可以保存 null 值。您可以为此提供任何帮助吗? (2认同)

zla*_*ajo 87

使用Boolean.valueOf(string)Boolean.parseBoolean(string)时必须小心.原因是如果String不等于"true",则方法将始终返回false(忽略大小写).

例如:

Boolean.valueOf("YES") -> false
Run Code Online (Sandbox Code Playgroud)

由于这种行为,我建议添加一些机制,以确保应转换为布尔值的字符串遵循指定的格式.

例如:

if (string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false")) {
    Boolean.valueOf(string)
    // do something   
} else {
    // throw some exception
}
Run Code Online (Sandbox Code Playgroud)

  • 这是我见过的最好的例子以及应该在布尔类型中实现的内容.抛出无效布尔值的异常对于许多应用程序很重要. (9认同)
  • 不,那不完全正确。这是 parseBoolean public static boolean parseBoolean(String s) { return ((s != null) && s.equalsIgnoreCase("true")); 的底层实现。} (2认同)

CJS*_*CJS 21

Boolean b = Boolean.valueOf(string);
Run Code Online (Sandbox Code Playgroud)

b如果字符串不是null并且等于true(忽略大小写),则值为true .


Phi*_*Lho 17

除了KLE的优秀答案之外,我们还可以提供更灵活的内容:

boolean b = string.equalsIgnoreCase("true") || string.equalsIgnoreCase("t") || 
        string.equalsIgnoreCase("yes") || string.equalsIgnoreCase("y") || 
        string.equalsIgnoreCase("sure") || string.equalsIgnoreCase("aye") || 
        string.equalsIgnoreCase("oui") || string.equalsIgnoreCase("vrai");
Run Code Online (Sandbox Code Playgroud)

(灵感来自zlajo的回答...... :-))


Zed*_*Zed 11

boolean b = string.equalsIgnoreCase("true");
Run Code Online (Sandbox Code Playgroud)


dav*_*xie 9

好吧,就像现在在2018年1月,最好的方法是使用apache BooleanUtils.toBoolean.

这会将任何类似boolean的字符串转换为boolean,例如Y,yes,true,N,no,false等.

真的很方便!


Aec*_*Liu 6

public static boolean stringToBool(String s) {
        s = s.toLowerCase();
        Set<String> trueSet = new HashSet<String>(Arrays.asList("1", "true", "yes"));
        Set<String> falseSet = new HashSet<String>(Arrays.asList("0", "false", "no"));

        if (trueSet.contains(s))
            return true;
        if (falseSet.contains(s))
            return false;

        throw new IllegalArgumentException(s + " is not a boolean.");
    }
Run Code Online (Sandbox Code Playgroud)

我将字符串转换为布尔值的方法。


Dha*_*ana 5

使用Apache Commons 库BooleanUtils

String[] values= new String[]{"y","Y","n","N","Yes","YES","yes","no","No","NO","true","false","True","False","TRUE","FALSE",null};
for(String booleanStr : values){
    System.out.println("Str ="+ booleanStr +": boolean =" +BooleanUtils.toBoolean(booleanStr));
}
Run Code Online (Sandbox Code Playgroud)

结果:

Str =N: boolean =false
Str =Yes: boolean =true
Str =YES: boolean =true
Str =yes: boolean =true
Str =no: boolean =false
Str =No: boolean =false
Str =NO: boolean =false
Str =true: boolean =true
Str =false: boolean =false
Str =True: boolean =true
Str =False: boolean =false
Str =TRUE: boolean =true
Str =FALSE: boolean =false
Str =null: boolean =false
Run Code Online (Sandbox Code Playgroud)

  • org.apache.commons.lang3.BooleanUtils 来自 Apache commons Lang API。 (2认同)