如何在 Java 中检查 String 值是否为布尔类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1762878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:24:06  来源:igfitidea点击:

How to check if String value is Boolean type in Java?

javastringboolean

提问by Ragnar

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.

关键是如果字符串值是“真”或“假”,则返回值应该是真。在所有其他值中,它应该是假的。

I tried these:

我试过这些:

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

All functions returned false :(

所有函数都返回 false :(

采纳答案by mfx

  • parseBoolean(String) returns true if the String is (case-insensitive) "true", otherwise false
  • valueOf(String) ditto, returns the canonical Boolean Objects
  • getBoolean(String) is a red herring; it fetches the System property of the given name and compares that to "true"
  • parseBoolean(String) 如果 String 是(不区分大小写)“true”,则返回 true,否则返回 false
  • valueOf(String) 同上,返回规范的布尔对象
  • getBoolean(String) 是一个红鲱鱼;它获取给定名称的 System 属性并将其与“true”进行比较

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

不存在测试字符串是否编码布尔值的方法;对于所有实际效果,任何非“真”字符串都是“假”。

回答by Ragnar

if ("true".equals(value) || "false".equals(value)) {
  // do something
} else {
  // do something else
}

回答by Bombe

return "true".equals(value) || "false".equals(value);

回答by dxh

return value.equals("false") || value.equals("true");

回答by Andreas Dolk

Yes, but, didn't you parse "false"? If you parse "true", then they return true.

是的,但是,您没有解析“假”吗?如果您解析“true”,则它们返回 true。

Maybe there's a misunderstanding: the methods don't test, if the String content represents a boolean value, they evaluate the String content to boolean.

也许有一个误解:这些方法不测试,如果 String 内容表示一个布尔值,它们会将 String 内容评估为布尔值。

回答by Thorsten Dittmar

The methods you're calling on the Booleanclass don't check whether the string contains a valid boolean value, but they return the boolean value that represents the contents of the string: put "true" in string, they return true, put "false" in string, they return false.

您在Boolean类上调用的方法不检查字符串是否包含有效的布尔值,但它们返回表示字符串内容的布尔值:将“true”放入字符串中,它们返回true,放入“false”在字符串中,它们返回false.

You can surely use these methods, however, to check for valid boolean values, as I'd expect them to throw an exception if the string contains "hello" or something not boolean.

但是,您肯定可以使用这些方法来检查有效的布尔值,因为如果字符串包含“hello”或非布尔值,我希望它们会抛出异常。

Wrap that in a Method ContainsBoolStringand you're go.

把它包装在一个方法中ContainsBoolString,你就可以走了。

EDIT
By the way, in C# there are methods like bool Int32.TryParse(string x, out int i)that perform the check whether the content can be parsed and then return the parsed result.

编辑
顺便说一句,在 C# 中有类似的方法bool Int32.TryParse(string x, out int i)执行检查内容是否可以解析,然后返回解析结果。

int i;
if (Int32.TryParse("Hello", out i))
  // Hello is an int and its value is in i
else
  // Hello is not an int

Benchmarks indicate they are way faster than the following:

基准测试表明它们比以下更快:

int i;
try
{
   i = Int32.Parse("Hello");
   // Hello is an int and its value is in i
}
catch
{
   // Hello is not an int
}

Maybe there are similar methods in Java? It's been a while since I've used Java...

也许Java中有类似的方法?我已经有一段时间没有使用 Java 了......

回答by Stuart

Something you should also take into consideration is character casing...

您还应该考虑的是字符大小写...

Instead of:

代替:

return value.equals("false") || value.equals("true");

Do this:

做这个:

return value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true");

回答by Dan Polites

I suggest that you take a look at the Java docs for these methods. It appears that you are using them incorrectly. These methods will not tell you if the string is a valid boolean value, but instead they return a boolean, set to true or false, based on the string that you pass in, "true" or "false".

我建议您查看这些方法的 Java 文档。看来您使用它们的方式不正确。这些方法不会告诉您该字符串是否为有效的布尔值,而是根据您传入的字符串“true”或“false”返回一个布尔值,设置为 true 或 false。

http://www.j2ee.me/javase/6/docs/api/java/lang/Boolean.html

http://www.j2ee.me/javase/6/docs/api/java/lang/Boolean.html

回答by Stefan Hendriks

Actually, checking for a Boolean typein a String (which is a type) is impossible. Basically you're asking how to do a 'string compare'.

实际上,检查字符串(类型)中的布尔类型是不可能的。基本上你是在问如何做一个“字符串比较”。

Like others stated. You need to define when you want to return "true" or "false" (under what conditions). Do you want it to be case(in)sensitive? What if the value is null?

就像其他人所说的那样。您需要定义何时要返回“真”或“假”(在什么条件下)。你想让它区分大小写吗?如果值为空怎么办?

I think Boolean.valueOf() is your friend, javadoc says:

我认为 Boolean.valueOf() 是你的朋友,javadoc 说:

Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.

返回一个布尔值,其值由指定的字符串表示。如果字符串参数不为 null 并且与字符串“true”相等(忽略大小写),则返回的布尔值表示值 true。

示例:Boolean.valueOf("True") 返回 true。
示例:Boolean.valueOf("yes") 返回 false。

回答by Raymond Lazarus

Can also do it by regex:

也可以通过正则表达式来实现:

Pattern queryLangPattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
Matcher matcher = queryLangPattern.matcher(booleanParam);
return matcher.matches();