|
-
Feb 21st, 2006, 06:51 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] String < AnotherString?
I have to tell if one string is less than another (e.g. First string is apple and second string is banana. first string will be less than second string because it comes before it in the alphabet. (at least i think that's how it works)).
It seems I can't do this with C# because it just gives
Operator '<' cannot be applied to operands of type 'string' and 'string'
which I seem to have found that you cannot do in C#.
So, how can you tell if one string would come alphabetically before another?
Thanks. Help will be greatly appreciated.
-
Feb 21st, 2006, 07:06 AM
#2
Re: String < AnotherString?
-
Feb 21st, 2006, 07:06 AM
#3
Re: String < AnotherString?
-
Feb 21st, 2006, 07:09 AM
#4
Thread Starter
Fanatic Member
Re: String < AnotherString?
Thanks, mendhak. I'll try it next time I'm at college (going home now).
I thought it might have been this, but I thought the compare only told you if it was equal to it or not... bit confused as to what it the returns as it's an integer and not a boolean.
Thanks again.
-
Feb 21st, 2006, 07:11 AM
#5
Re: String < AnotherString?
Well you can use the static method Compare of string class to compare two strings. If the compare method returns value less than 0 then that means first string is less that the second string and if you get a return value of greater than 0 then the first string is greater than the second string. Take a look at this
Code:
string s1 = "apple";
string s2 = "banana";
if (string.Compare(s1, s2) < 0)
MessageBox.Show(s1 + " comes before " + s2);
else
MessageBox.Show(s2 + " comes before " + s1);
Edit --
Didin't see Altaf's post ealier.
Last edited by Shuja Ali; Feb 21st, 2006 at 07:14 AM.
Reason: Didn't see Altaf's post
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 23rd, 2006, 06:58 AM
#6
Thread Starter
Fanatic Member
Re: String < AnotherString?
 Originally Posted by Shuja Ali
Well you can use the static method Compare of string class to compare two strings. If the compare method returns value less than 0 then that means first string is less that the second string and if you get a return value of greater than 0 then the first string is greater than the second string. Take a look at this
Code:
string s1 = "apple";
string s2 = "banana";
if (string.Compare(s1, s2) < 0)
MessageBox.Show(s1 + " comes before " + s2);
else
MessageBox.Show(s2 + " comes before " + s1);
Edit --
Didin't see Altaf's post ealier. 
Thank you very much for the help! I understand the Compare function now. Works great.
Thanks mendhak, too. Resolved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|