【发布时间】:2010-12-05 01:20:58
【问题描述】:
如何将String对象转换为Boolean对象?
【问题讨论】:
-
字符串的值是多少?
-
您对如何将字符串转换为布尔值有什么期望?
-
myvartypeboolean = !!valuetoconvert
如何将String对象转换为Boolean对象?
【问题讨论】:
尝试(取决于您想要的结果类型):
Boolean boolean1 = Boolean.valueOf("true");
boolean boolean2 = Boolean.parseBoolean("true");
优势:
Boolean.TRUE 或Boolean.FALSE 的两个实例。官方文档在Javadoc。
更新:
也可以使用自动装箱,但会降低性能。
我建议仅在您必须自己施放时才使用它,而不是在施放可以避免时使用。
【讨论】:
boolean boolean2 = Boolean.valueOf("true");会怎样
在使用 Boolean.valueOf(string) 或 Boolean.parseBoolean(string) 时必须小心。这样做的原因是,如果 String 不等于“true”(忽略大小写),方法将始终返回 false。
例如:
Boolean.valueOf("YES") -> false
由于这种行为,我建议添加一些机制来确保应转换为布尔值的字符串遵循指定的格式。
例如:
if (string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false")) {
Boolean.valueOf(string)
// do something
} else {
// throw some exception
}
【讨论】:
Boolean b = Boolean.valueOf(string);
如果字符串不为空且等于true(忽略大小写),则b 的值为真。
【讨论】:
除了 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");
(受 zlajo 的回答启发... :-))
【讨论】:
boolean b = string.equalsIgnoreCase("true");
【讨论】:
嗯,就像现在在 2018 年 1 月一样,最好的方法是使用 apache 的 BooleanUtils.toBoolean。
这会将任何类似布尔值的字符串转换为布尔值,例如Y、是、真、N、否、假等
真的很方便!
【讨论】:
使用Apache Commons library BooleanUtils class:
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));
}
结果:
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
【讨论】:
BooleanUtils 仍然是错误的(虽然比Boolean 方法少),因为如果值既不被识别为true 或false,它也不会抛出异常。所以ture(某人在某处打错字)仍然被盲目地假设为false。我在 Guava 中也找不到任何东西(就像 23.0 时没有 Booleans.stringConverter 一样)。
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.");
}
我将字符串转换为布尔值的方法。
【讨论】:
我就是这样做的:
"1##true".contains( string )
我的情况大多是 1 或 true。我使用哈希作为分隔符。
【讨论】:
要获取字符串的布尔值,试试这个:
public boolean toBoolean(String s) {
try {
return Boolean.parseBoolean(s); // Successfully converted String to boolean
} catch(Exception e) {
return null; // There was some error, so return null.
}
}
如果有错误,它将返回null。 示例:
toBoolean("true"); // Returns true
toBoolean("tr.u;e"); // Returns null
【讨论】:
parseBoolean(String s) 不会引发异常。
为什么不使用正则表达式?
public static boolean toBoolean( String target )
{
if( target == null ) return false;
return target.matches( "(?i:^(1|true|yes|oui|vrai|y)$)" );
}
【讨论】:
我们创建了soyuz-to 库来简化这个问题(将 X 转换为 Y)。这只是针对类似问题的一组 SO 答案。将库用于这样一个简单的问题可能很奇怪,但在很多类似的情况下确实很有帮助。
import io.thedocs.soyuz.to;
Boolean aBoolean = to.Boolean("true");
请检查一下 - 它非常简单,还有很多其他有用的功能
【讨论】:
访问http://msdn.microsoft.com/en-us/library/system.boolean.parse.aspx
这会让你知道该怎么做。
这是我从Java documentation 得到的:
方法详情
parseBoolean
public static boolean parseBoolean(String s)将字符串参数解析为布尔值。如果字符串参数不是
null并且忽略大小写等于字符串“true”,则返回的布尔值表示值 true。参数:
s- 包含要解析的布尔表示的字符串返回:字符串参数表示的布尔值
自: 1.5
【讨论】:
您可以通过 System 类直接设置布尔值等价于任何字符串 并在任何地方访问它..
System.setProperty("n","false");
System.setProperty("y","true");
System.setProperty("yes","true");
System.setProperty("no","false");
System.out.println(Boolean.getBoolean("n")); //false
System.out.println(Boolean.getBoolean("y")); //true
System.out.println(Boolean.getBoolean("no")); //false
System.out.println(Boolean.getBoolean("yes")); //true
【讨论】: