如何在 ActionScript 中将字符串转换为布尔值? [英] How do you convert a String into a Boolean in ActionScript?
本文介绍了如何在 ActionScript 中将字符串转换为布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
var bool:String = "true";
没有 if 块或 switch 语句,如何将其转换为布尔对象?</p>
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;
这只是无缘无故地使代码复杂化,因为评估发生在部分:
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
这篇关于如何在 ActionScript 中将字符串转换为布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文