Is there a string compareTo() ?

Jan 19, 2011 at 3:44pm
Hi!

I'm wondering if there's a similar function to the Java String::compareTo() function?? E.g. one that will not only compare the 2 strings but also take into account the alphabetic ordering.

If you're not familiar with this, imagine comparing 2 strings
1
2
3
4
string str1 = "Hello";
string str2 = "Yo!";

int result = str1.compareTo(str2);


Result will become a negative value since "Hello" comes before "Yo" in an alphabetic order. Comparing str2.compareTo(str1) would generate a positive value.

Does this exists in C++?
Jan 19, 2011 at 3:47pm
str1 > str2 would evaluate to false.
Jan 19, 2011 at 3:49pm
strcmp for c-style strings
std::compare for std::string objects and c-style strings
Last edited on Jan 19, 2011 at 3:50pm
Jan 19, 2011 at 3:54pm
1
2
3
4
string str1 = "Hello";
string str2 = "Yo!";

int result = str1.compare(str2);


http://www.cplusplus.com/reference/string/string/compare/ ?
Jan 19, 2011 at 5:21pm
Oh! That simple. Thanks :)
Topic archived. No new replies allowed.