简体   繁体   中英

How to change String value comparison

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM