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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With