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

10,883

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
Share:
10,883
flreey
Author by

flreey

A Software Engineer struggling through the interweb one byte at a time.

Updated on June 04, 2022

Comments

  • flreey
    flreey about 2 years

    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?

  • Sam DeHaan
    Sam DeHaan over 12 years
    @Marcx putting an emoticon doesn't make you right. sch is right, you aren't.
  • Marcx
    Marcx over 12 years
    I don't think, his reply is not correct... boolValue2 should be ` false according to his example, but in realty it is true...
  • sch
    sch over 12 years
    @Marcx - I tested the code before posting it and boolValue2 is false as expected.