1

In my code I need to compare string letters but my problem is that lower case letters are greater than upper case letter.

For example Z < a.

How could I implement this in my code ?

Thanks

flq
  • 22,247
  • 8
  • 55
  • 77
Bernard Larouche
  • 1,211
  • 2
  • 13
  • 22

4 Answers4

3
String.Compare(string1, string2, false);

String.Compare will do a string comparisson and ignore their case. It returns an integer representing the match.

Using your example:

// Will result in true
String.Compare("Z", "a", false) > 0
Josh
  • 44,706
  • 7
  • 102
  • 124
  • he said, that he'd like to compare chars, not a whole string. therefore he'd need to browse through the string maybe with charAt and do the comparision then... – Gnark Mar 08 '10 at 15:51
  • Either way String.Compare will work. If you pass in a single character it will work, or if you pass in something like "Bob" it will do an ordinal comparisson and return their lexical relationship. This is what .Net will use under the hood when sorting an array of strings. – Josh Mar 08 '10 at 15:54
3

just use :

String.Compare( string , string , StringComparison)

with the StringComaparison set to InvariantCultureIgnoreCase , OrdinalIgnoreCase or CurrentCultureIgnoreCase depending on context....

http://msdn.microsoft.com/en-us/library/e6883c06.aspx

Mesh
  • 6,262
  • 5
  • 34
  • 53
2

What if you make them both uppercase and compare afterwards ? (I guess you do not want to test equality)

thelost
  • 6,638
  • 4
  • 28
  • 44
0

Are you using char's in the end? If yes, you could just remap the ranges of the integer values of the characters.

Marcel
  • 15,039
  • 20
  • 92
  • 150