Sign in to follow this  
darenking

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

Recommended Posts

darenking    122
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?

Share this post


Link to post
Share on other sites
snk_kid    1312
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.

Share this post


Link to post
Share on other sites
anonuser    148
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.

Share this post


Link to post
Share on other sites
darenking    122
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.

Share this post


Link to post
Share on other sites
darenking    122
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'.

Share this post


Link to post
Share on other sites
_the_phantom_    11250
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;
}

Share this post


Link to post
Share on other sites
Perost    332
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.

Share this post


Link to post
Share on other sites
Gink    100
Quote:
Original post by darenking
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'.



if you want to check the first 2 chars in phrase then write this
string.substr(0,2) == "//"

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Gink

if you want to check the first 2 chars in phrase then write this
string.substr(0,2) == "//"


The only problem with that method is your creating a temporary which is redundant.

Share this post


Link to post
Share on other sites
xegoth    154
You might want to consider posting your code. == does work for C++ strings.


string val = "hello"

if(val == "hello")
{
return true;
}
else
{
return false;
}



The above example would return true.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this