Jump to content
  • Advertisement
Sign in to follow this  
darenking

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

This topic is 4822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!