string java-me:将字符串转换为布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5428767/
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
java-me: Convert String to boolean
提问by Jose S
I'm developing for BlackBerry and I got stuck with this stupid problem:
我正在为 BlackBerry 开发,但遇到了这个愚蠢的问题:
I need to convert string values "1" and "0" to true and false, respectively. Nevertheless, Blackberry JDK is based in Java 1.3, so I can't use Boolean.parseBoolean, Boolean.valueOf or Boolean.getValue.
我需要将字符串值“1”和“0”分别转换为 true 和 false。尽管如此,Blackberry JDK 基于 Java 1.3,所以我不能使用 Boolean.parseBoolean、Boolean.valueOf 或 Boolean.getValue。
Obviously I can do something like:
显然我可以做这样的事情:
if (str.equals("1")) return true;
else if (str.equals("0")) return false;
But this looks very ugly and maybe these string values could change to "true" and "false" later. So, Is there another way to convert between these types (String -> boolean, Java 1.3)?
但这看起来非常难看,也许这些字符串值以后可能会更改为“true”和“false”。那么,是否有另一种方法可以在这些类型之间进行转换(字符串 -> 布尔值,Java 1.3)?
UPDATED: all the answers of this question was very helpfully but I needed to mark one, so I selected Ishtar's answer.
更新:这个问题的所有答案都非常有帮助,但我需要标记一个,所以我选择了 Ishtar 的答案。
Even so, my fix was a combination of multiple answers.
即便如此,我的解决方法是多个答案的组合。
采纳答案by Ishtar
public static boolean stringToBool(String s) {
if (s.equals("1"))
return true;
if (s.equals("0"))
return false;
throw new IllegalArgumentException(s+" is not a bool. Only 1 and 0 are.");
}
If you later change it to "true/false", you won't accidentally order 28,000 tons of coal. Calling with the wrong parameter will throw an exception, instead of guessing and returning false. In my opinion "pancake"
is not false
.
如果稍后将其更改为“true/false”,则不会意外订购28,000 吨煤。使用错误的参数调用将抛出异常,而不是猜测并返回 false。在我看来"pancake"
不是false
。
回答by Brian Roach
If you don't have Boolean.valueOf(String s)
... yeah, that's pretty much it. I'd define your own static method like:
如果你没有Boolean.valueOf(String s)
......是的,差不多就是这样。我会定义你自己的静态方法,如:
public static boolean booleanFromString(String s)
{
return s.equals("1");
}
That would solve your "May change to true or false later" problem as you could add/change that in the method and not have to change anything else in your code.
这将解决您的“以后可能会更改为 true 或 false”的问题,因为您可以在方法中添加/更改它,而不必更改代码中的任何其他内容。
回答by Prince John Wesley
maybe these string values could change to "true" and "false" later
也许这些字符串值稍后会更改为“true”和“false”
Don't hard code your parameter.
不要硬编码你的参数。
So define your own method like this.
所以像这样定义你自己的方法。
public static final String TRUE = "true"; //"1"
public static boolean strToBool(String s) {
// don't hard code your parameter.
return str.equalsIgnoreCase(TRUE);
}
回答by Buhake Sindi
Java's Boolean
object (if I remember correctly) has already 2 constants:
Java 的Boolean
对象(如果我没记错的话)已经有 2 个常量:
Boolean.TRUE
Boolean.FALSE
Boolean.TRUE
Boolean.FALSE
you can use Boolean.booleanValue()
to return it's corresponding boolean
value.
您可以使用Boolean.booleanValue()
返回它的相应boolean
值。
You can create your own valueOf(String s)
method to return a boolean
and/or Boolean
like so:
您可以创建自己的valueOf(String s)
方法来返回 aboolean
和/或Boolean
像这样:
public static boolean toBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
public static Boolean valueOf(String s) {
return (toBoolean(s)? Boolean.TRUE : Boolean.FALSE);
}
回答by y0rk
you should check null and whitespace chars. remove them and check value.
您应该检查空字符和空白字符。删除它们并检查值。
return (str!=null && str.trim().equals("1"));
回答by AechoLiu
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.");
}
Enhance Ishfaranswer.
增强Ishfar 的答案。