How to Compare two strings??

I want a method to compare two strings in c++. Is there a method like ".equals()" in java to compare strings in c++??

I got stuck with this matter in my program to go further.

Waiting for a suitable reply.
You simply use operator== to compare them (assuming we're talking about std::string here and not C strings).
Can I use it this way?

1
2
3
4
string a="XXXXXXXXX";
 if(a=="YYYYYY")
       then //do something
else // do something 


I want to use string comparison in this way....
Yes you can do it like that. You can also use the built in comparison functions:

1
2
3
4
5
6
string a = "XXX";

if(!a.compare("YYY") // function returns 0 if strings are equal hence the '!'
  std::cout << "They are the same.\n";
else
  std::cout << "They are not.\n"
Last edited on
Topic archived. No new replies allowed.