由网友(想再一次追寻当初的那个梦)分享简介:我有以下的code:var bool:String = "true";没有的,如果块或switch语句,这可怎么转换成一个Boolean对象?Without an if block or switch statement, how can this be converted into a Boolean objec...
我有以下的code:
var bool:String = "true";
没有的,如果块或switch语句,这可怎么转换成一个Boolean对象?
Without an if block or switch statement, how can this be converted into a Boolean object?
推荐答案
您可以使用:
var boolString:String = "true";
var boolValue:Boolean = boolString == "true"; // true
var boolString2:String = "false";
var boolValue2:Boolean = boolString2 == "true"; // false
修改
下面留言建议使用
var boolValue:Boolean = (boolString == "true") ? true : false;
这仅仅是复杂的code无缘无故作为评价发生在部分:
This is just complicating the code for no reason as the evaluation happens in the part:
(boolString == "true")
使用三元操作等效于:
Using the ternary operator is equivalent to:
var tempValue:Boolean = boolString == "true"; // returns true: this is what I suggested
var boolValue:Boolean = tempValue ? true : false; // this is redundant
相关推荐
最新文章