25

I am currently reading from an ini file with a key/value pair. i.e.

isValid = true

When get the key/value pair I need to convert a string of 'true' to a bool. Without using boost what would be the best way to do this?

I know I can so a string compare on the value ("true", "false") but I would like to do the conversion without having the string in the ini file be case sensitive.

Thanks

Wesley
  • 2,921
  • 6
  • 27
  • 30

5 Answers5

36

Another solution would be to use tolower() to get a lower-case version of the string and then compare or use string-streams:

#include <sstream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cctype>

bool to_bool(std::string str) {
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    std::istringstream is(str);
    bool b;
    is >> std::boolalpha >> b;
    return b;
}

// ...
bool b = to_bool("tRuE");
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
3
#include <string>
#include <strings.h>
#include <cstdlib>
#include <iostream>

bool
string2bool (const std::string & v)
{
    return !v.empty () &&
        (strcasecmp (v.c_str (), "true") == 0 ||
         atoi (v.c_str ()) != 0);
}

int
main ()
{
    std::string s;
    std::cout << "Please enter string: " << std::flush;
    std::cin >> s;
    std::cout << "This is " << (string2bool (s) ? "true" : "false") << std::endl;
}

An example input and output:

$ ./test 
Please enter string: 0
This is false
$ ./test 
Please enter string: 1
This is true
$ ./test 
Please enter string: 3
This is true
$ ./test 
Please enter string: TRuE
This is true
$ 
1

If you can't use boost, try strcasecmp:

#include <cstring>

std::string value = "TrUe";

bool isTrue = (strcasecmp("true",value.c_str()) == 0);
Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
  • `strcasecmp` doesn't exist under Windows. – zneak Aug 31 '10 at 21:25
  • 1
    Oh, come on! It sounds like a Boost is the only way to go to compare strings :-) There are much more easiest and efficient ways then doing lexical cast, really. Especially when you know exactly what you are doing and not creating an all-purpose-all-in-one conversion library. –  Aug 31 '10 at 21:25
  • @zneak - Windows has "stricmp" though (http://msdn.microsoft.com/en-us/library/k59z8dwe%28v=VS.80%29.aspx) –  Aug 31 '10 at 21:27
  • strcmpi is POSIX standard, with the silly deprecation but still working behavior in Visual Studio. – Joel Aug 31 '10 at 21:30
  • `stricmp` is more widely used for case insensitive compare of C-style strings. – Eugen Constantin Dinca Aug 31 '10 at 21:32
0

Lowercase the string by iterating the string and calling tolower on the carachters, then compare it to "true" or "false", if casing is your only concern.

for (std::string::iterator iter = myString.begin(); iter != myString.end(); iter++)
    *iter = tolower(*iter);
zneak
  • 134,922
  • 42
  • 253
  • 328
  • tolower() works on a single character, c_str() returns a pointer to a string that must not be modified. So this idea would actually be harmful. – Uli Schlachter Aug 31 '10 at 21:25
  • @Uli Schlanchter Where the hell did I get the idea it worked on strings? Thanks for the catch. – zneak Aug 31 '10 at 21:26
0

Suggestions for case-insenstive string comparisions on C++ strings can be found here: Case insensitive string comparison in C++

Community
  • 1
  • 1
Uli Schlachter
  • 9,337
  • 1
  • 23
  • 39