how to convert a string to a bool

Sachin Kainth picture Sachin Kainth · Mar 16, 2012 · Viewed 224.6k times · Source

I have a string that can be either "0" or "1", and it is guaranteed that it won't be anything else.

So the question is: what's the best, simplest and most elegant way to convert this to a bool?

Answer

Kendall Frey picture Kendall Frey · Mar 16, 2012

Quite simple indeed:

bool b = str == "1";