Convert string to bool in C++

In this article we will discuss different ways to convert a string value to a boolean value in C++.

Table Of Contents

Introduction

Suppose you have a numeric value in string, and it is not “0”, then it will be converted to true. Whereas, if you have number zero in the string i.e. “0”, then it will be converted to false. Also, if you have non numeric string, then it will also be converted to false.

There are different ways to do this. Let’s discuss them one by one

Method 1: Using istringstream

Create a istringstream object, and initialize it with the given string value. Then fetch the boolean value from this istringstream object. The bool value will be true, if string had a numeric value, but not 0. Otherwise, fetched bool value will be false.

Let’s see an example, where we will convert string “1” to bool value.

Example 1

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string strValue = "1";

    bool flag;

    // If string is not a number then returns false
    std::istringstream(strValue) >> flag;

    std::cout << std::boolalpha << flag << std::endl;

    return 0;
}

Output:

true

The string value “1” got converted to bool value true.

Example 2

Let’s see another example, where we will convert string “0” to bool value.

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string strValue = "0";

    bool flag;

    // If string is not a number then returns false
    std::istringstream(strValue) >> flag;

    std::cout << std::boolalpha << flag << std::endl;

    return 0;
}

Output:

false

The string value “0” got converted to bool value false.

Method 2: Using == operator

We can directly use the == operator of string object to check, if string value is “0” or not. If it is “0”, then it will be converted to bool value false, otherwise it will be converted to bool value true.

Let’s see some examples,

Example 1

Let’s see another example, where we will convert string “1” to bool value.

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string strValue = "1";

    // If string is not "0" then returns true
    bool flag = !(strValue == "0");

    std::cout << std::boolalpha << flag << std::endl;

    return 0;
}

Output:

true

The string value “1” got converted to bool value true.

Example 2

Let’s see another example, where we will convert string “0” to bool value.

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string strValue = "0";

    // If string is not "0" then returns true
    bool flag = !(strValue == "0");

    std::cout << std::boolalpha << flag << std::endl;

    return 0;
}

Output:

false

The string value “0” got converted to bool value false.

Summary

We learned about different ways to convert a string value to boll value in C++. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top