Converting from a std::string to bool

C++StringBoolean

C++ Problem Overview


What is the best way to convert a std::string to bool? I am calling a function that returns either "0" or "1", and I need a clean solution for turning this into a boolean value.

C++ Solutions


Solution 1 - C++

I am surprised that no one mentioned this one:

bool b;
istringstream("1") >> b;

or

bool b;
istringstream("true") >> std::boolalpha >> b;

Solution 2 - C++

bool to_bool(std::string const& s) {
     return s != "0";
}

Solution 3 - C++

It'll probably be overkill for you, but I'd use boost::lexical_cast

boost::lexical_cast<bool>("1") // returns true
boost::lexical_cast<bool>("0") // returns false

Solution 4 - C++

Either you care about the possibility of an invalid return value or you don't. Most answers so far are in the middle ground, catching some strings besides "0" and "1", perhaps rationalizing about how they should be converted, perhaps throwing an exception. Invalid input cannot produce valid output, and you shouldn't try to accept it.

If you don't care about invalid returns, use s[0] == '1'. It's super simple and obvious. If you must justify its tolerance to someone, say it converts invalid input to false, and the empty string is likely to be a single \0 in your STL implementation so it's reasonably stable. s == "1" is also good, but s != "0" seems obtuse to me and makes invalid => true.

If you do care about errors (and likely should), use

if ( s.size() != 1
 || s[0] < '0' || s[0] > '1' ) throw input_exception();
b = ( s[0] == '1' );

This catches ALL errors, it's also bluntly obvious and simple to anyone who knows a smidgen of C, and nothing will perform any faster.

Solution 5 - C++

There is also std::stoi in c++11:

bool value = std::stoi(someString.c_str());

Solution 6 - C++

I'd use this, which does what you want, and catches the error case.

bool to_bool(const std::string& x) {
  assert(x == "0" || x == "1");
  return x == "1";
}

Solution 7 - C++

DavidL's answer is the best, but I find myself wanting to support both forms of boolean input at the same time. So a minor variation on the theme (named after std::stoi):

bool stob(std::string s, bool throw_on_error = true)
{
    auto result = false;    // failure to assert is false

    std::istringstream is(s);
    // first try simple integer conversion
    is >> result;

    if (is.fail())
    {
        // simple integer failed; try boolean
        is.clear();
        is >> std::boolalpha >> result;
    }

    if (is.fail() && throw_on_error)
    {
        throw std::invalid_argument(s.append(" is not convertable to bool"));
    }

    return result;
}

This supports "0", "1", "true", and "false" as valid inputs. Unfortunately, I can't figure out a portable way to also support "TRUE" and "FALSE"

Solution 8 - C++

Write a free function:

bool ToBool( const std::string & s ) {
   return s.at(0) == '1';
}

This is about the simplest thing that might work, but you need to ask yourself:

  • what should an empty string return? the version above throws an exception
  • what should a character other than '1' or '0' convert to?
  • is a string of more than one character a valid input for the function?

I'm sure there are others - this is the joy of API design!

Solution 9 - C++

I'd change the ugly function that returns this string in the first place. That's what bool is for.

Solution 10 - C++

Try this:

bool value;

if(string == "1")
    value = true;
else if(string == "0")
    value = false;
    

Solution 11 - C++

bool to_bool(std::string const &string) { 
    return string[0] == '1';
}

Solution 12 - C++

Here's a way similar to Kyle's except it handles the leading zeroes and stuff:

bool to_bool(std::string const& s) {
     return atoi(s.c_str());
}

Solution 13 - C++

You could always wrap the returned string in a class that handles the concept of boolean strings:

class BoolString : public string
{
public:
    BoolString(string const &s)
    :   string(s)
    {
        if (s != "0" && s != "1")
        {
            throw invalid_argument(s);
        }
    }
    
    operator bool()
    {
        return *this == "1";
    }
}

Call something like this:

BoolString bs(func_that_returns_string());
if (bs) ...;
else ...;

Which will throw invalid_argument if the rule about "0" and "1" is violated.

Solution 14 - C++

If you need "true" and "false" string support consider Boost...

BOOST_TEST(convert<bool>( "true", cnv(std::boolalpha)).value_or(false) ==  true);
BOOST_TEST(convert<bool>("false", cnv(std::boolalpha)).value_or( true) == false);

BOOST_TEST(convert<bool>("1", cnv(std::noboolalpha)).value_or(false) ==  true);
BOOST_TEST(convert<bool>("0", cnv(std::noboolalpha)).value_or( true) == false);

https://www.boost.org/doc/libs/1_71_0/libs/convert/doc/html/boost_convert/converters_detail/stream_converter.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncquillenView Question on Stackoverflow
Solution 1 - C++David L.View Answer on Stackoverflow
Solution 2 - C++Chris Jester-YoungView Answer on Stackoverflow
Solution 3 - C++Kornel KisielewiczView Answer on Stackoverflow
Solution 4 - C++PotatoswatterView Answer on Stackoverflow
Solution 5 - C++MikeView Answer on Stackoverflow
Solution 6 - C++user97370View Answer on Stackoverflow
Solution 7 - C++jwmView Answer on Stackoverflow
Solution 8 - C++anonView Answer on Stackoverflow
Solution 9 - C++rmnView Answer on Stackoverflow
Solution 10 - C++Kyle LutzView Answer on Stackoverflow
Solution 11 - C++Jerry CoffinView Answer on Stackoverflow
Solution 12 - C++Mahmoud Al-QudsiView Answer on Stackoverflow
Solution 13 - C++Matt JoinerView Answer on Stackoverflow
Solution 14 - C++kervinView Answer on Stackoverflow