如何检查是否字符串值是布尔类型在Java中?

如何检查是否字符串值是布尔类型在Java中?

问题描述:

我做了这一点了搜索,但没有找到什么有用的东西。

I did a little search on this but couldn't find anything useful.

问题的关键是,如果字符串值是真或假的返回值应该是真实的。在每一个其它的值应该是假的。

The point being that if String value is either "true" or "false" the return value should be true. In every other value it should be false.

我尝试这些:

String value = "false";
System.out.println("test1: " + Boolean.parseBoolean(value));
System.out.println("test2: " + Boolean.valueOf(value));
System.out.println("test3: " + Boolean.getBoolean(value));

所有的函数返回false:(

All functions returned false :(


  • parseBoolean(字符串)如果该字符串(不区分大小写)真返回true,否则为false

  • 的valueOf(字符串)同上,返回规范布尔对象

  • getBoolean(字符串)是红鲱鱼;它获取给定名称的系统属性,并比较了为true

有不存在任何方法来测试是否EN codeS布尔的字符串;对于所有的实际效果,任何非真-STRING为假。

There exists no method to test whether a String encodes a Boolean; for all practical effects, any non-"true"-String is "false".