Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 323
Randyaa Avatar asked Mar 20 '12 13:03

Randyaa


People also ask

Can Boolean be string?

The string representation of a Boolean is either "True" for a true value or "False" for a false value. The string representation of a Boolean value is defined by the read-only TrueString and FalseString fields. You use the ToString method to convert Boolean values to strings.


1 Answers

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
like image 55
sch Avatar answered Sep 30 '22 02:09

sch