| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"al" |
| Date: |
20 Dec 2003 07:23:56 PM |
| Object: |
Compare strings |
To compare two strings, not just to see if they are "equal" ("abcd"="abcd")
but tell their alphabetical order:
string a, b;
a = "abcd";
b = "bbcd";
Can all the ways below to do such comparison?
1. a > b
2. a.compare(b))
3. strcmp(a, b)
4. max(a, b)
Which one you prefer and why?
Thanks!
.
|
|
| User: "Cy Edmunds" |
|
| Title: Re: Compare strings |
20 Dec 2003 08:16:46 PM |
|
|
"al" <allin@168.net> wrote in message
news:MC6Fb.502440$0v4.21709853@bgtnsc04-news.ops.worldnet.att.net...
To compare two strings, not just to see if they are "equal"
("abcd"="abcd")
but tell their alphabetical order:
string a, b;
a = "abcd";
b = "bbcd";
Can all the ways below to do such comparison?
1. a > b
This is good. What could be simpler?
2. a.compare(b))
Reminds me of a Fortran II IF statement, and that isn't a glowing
recommendation. :)
3. strcmp(a, b)
I don't like this one because it won't work. However if you ammend it to
strcmp(a.c_str(), b.c_str())
I still don't like it. Using char * as a string type is a leftover from C.
4. max(a, b)
This is good too, if that's what you mean. It means something different than
#1 of course.
Which one you prefer and why?
Thanks!
--
Cy
http://home.rochester.rr.com/cyhome/
.
|
|
|
|

|
Related Articles |
|
|