[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.
Re: String < AnotherString?
Re: String < AnotherString?
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.
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. :(
Re: String < AnotherString?
Quote:
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.