How do you convert a String into a Boolean in ActionScript?

心不动则不痛 提交于 2019-12-12 11:15:11

问题


I have the following code:

var bool:String = "true";

Without an if block or switch statement, how can this be converted into a Boolean object?


回答1:


You can use:

var boolString:String = "true";
var boolValue:Boolean = boolString == "true"; // true
var boolString2:String = "false";
var boolValue2:Boolean = boolString2 == "true"; // false

Edit

A comment below suggests using

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


来源:https://stackoverflow.com/questions/9788234/how-do-you-convert-a-string-into-a-boolean-in-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!