Randyaa
Randyaa

Reputation: 2945

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?

Upvotes: 6

Views: 11176

Answers (1)

sch
sch

Reputation: 27506

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

Upvotes: 18

Related Questions