如何检查是否字符串值是布尔类型在Java中? [英] How to check if String value is Boolean type in Java?

查看:2162
本文介绍了如何检查是否字符串值是布尔类型在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".

这篇关于如何检查是否字符串值是布尔类型在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆