How to Compare two strings??

Jul 17, 2010 at 12:57pm
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.
Jul 17, 2010 at 1:05pm
You simply use operator== to compare them (assuming we're talking about std::string here and not C strings).
Jul 17, 2010 at 2:20pm
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....
Jul 17, 2010 at 2:53pm
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 Jul 17, 2010 at 2:53pm
Topic archived. No new replies allowed.