C++, how do I compare strings? (not c-style)

Started by
13 comments, last by Telastyn 18 years, 10 months ago
Such a simple thing, but after half an hour on google I still can't find it! I just want to test to see if a C++ string object holds (is equal to) a certain phrase. I've tried if ( phrase == "hello" ) { //whatever } where phrase is a C++ string object, but although it compiles it doesn't work. How do I test to see if the string object called 'phrase' holds the word 'hello'? Also how do I check if the first two characters in 'phrase' equal something, eg if they are both slashes: '//' ? For this do I use phrase.find?
Advertisement
Doesn't the string object have a member function for that, such as equal or compare?


Quote:Original post by darenking
where phrase is a C++ string object, but although it compiles it doesn't work.


equality operator is overloaded for std::basic_string so that is why it compiles, are searching for a sub-string in a string?

Quote:Original post by darenking
How do I test to see if the string object called 'phrase' holds the word 'hello'?


Is phrase a sentance where it could contain the sub-string hello? in which case use the family of find member functions defined for basic_string.

Quote:Original post by darenking
Also how do I check if the first two characters in 'phrase' equal something, eg if they are both slashes: '//' ? For this do I use phrase.find?


Yes, that would be searching a sub-string with-in a string, read this for all the different find/replace etc member functions.
http://cplusplus.com/ref/cstring/strcmp.html
Quote:Original post by jumpjumpjump
http://cplusplus.com/ref/cstring/strcmp.html


If you read carefully he is using C++ style strings not C-style strings and wrightly soo.
For the finding, you may want to look at regular expressions. They are wonderful for pattern matching, such as if a string starts with two // or if a # is included anywhere on the line.

You could do this with substring routines and looking at the position according to the length of the string.
Quote:Original post by snk_kid
Is phrase a sentance where it could contain the sub-string hello? in which case use the family of find member functions defined for basic_string.



No, I want to check if the string equals the word 'hello' exactly, hence I thought I could do it with =="hello"

It should be such a simple thing but I can't seem to do it.
OK, I can do it like this:

if ( line.find("hello") == 0 )

This checks to see if the string contains "hello" starting at the start of the string (position 0).

Works just fine, though I do wonder if there's a string method that checks to see if the string is exactly equal to 'hello'. The above would return 0 if the string is 'hello' but it will also return 0 if the string is 'hellogoodbye'.

ah you 100% its not working? As I've got C++ code which does what you want todo perfectly (its in production code and running right now)

std::string key((char*)&data.at(offset));if (key == "hostname"){	server.name.assign((char*)&data.at(offset));	offset += server.name.length() +1;}
string == "value" should work just fine. If this doesn't work, either your implementation of stl is really weird, or you're doing something really weird with your strings.

This topic is closed to new replies.

Advertisement