And use StringComparison.OrdinalIgnoreCase if your strings didn't come from user input. It'll ignore culture rules and is the fastest comparison option.
Ferdi265 ยท 2 points ยท Posted at 12:15:45 on August 20, 2016 ยท (Permalink)
And if they DO come from user input, but it's an application that takes user input over some network source (I.E. web), using CurrentCulture is a great way to add bugs if you use the software on computers with a different locale
Also, the Dictionary class has a constructor that takes in a comparator. This can be used to create a case-insensitive dictionary.
[deleted] ยท 25 points ยท Posted at 17:55:51 on June 18, 2016 ยท (Permalink)
[deleted]
Spire ยท -5 points ยท Posted at 20:40:34 on June 19, 2016 ยท (Permalink)
Lowercase text typically uses one extra bit per alphabetic character, so converting to lowercase rather than uppercase is not recommended on systems with limited RAM, such as embedded systems.
o11c ยท 2 points ยท Posted at 21:03:12 on June 19, 2016 ยท (Permalink)
Also, both ToUpper and ToLower would be wrong, you need the CaseFold operation (I'm not sure how to do this in C#). For example, Greek has two different lowercase sigmas (ฯ and ฯ) which should compare equal when you are ignoring case, but unchanged when converting to NFC (or even NFKC) and to lowercase.
Also note that the uppercase of a single character might not be a single character - for example, German lowercase ร gets uppercased as SS
Then there's the fact that the comparison depends on locale (the canonical example being the dotless vs dotted i in Turkish).
You can do a culture specific comparison in .NET pretty easy. There are overloads on String.Equals that you can pass in parameters to either compare without culture, with an 'invariant' culture, with the user's current culture, or with a specific culture.
.NET has pretty good support for culture specific text, date, and time comparisons.
987414567412369 ยท 15 points ยท Posted at 14:00:57 on June 19, 2016 ยท (Permalink)
And use
StringComparison.OrdinalIgnoreCaseif your strings didn't come from user input. It'll ignore culture rules and is the fastest comparison option.Ferdi265 ยท 2 points ยท Posted at 12:15:45 on August 20, 2016 ยท (Permalink)
And if they DO come from user input, but it's an application that takes user input over some network source (I.E. web), using CurrentCulture is a great way to add bugs if you use the software on computers with a different locale
boriszerussian ยท 12 points ยท Posted at 18:54:03 on June 18, 2016 ยท (Permalink)
Also, the Dictionary class has a constructor that takes in a comparator. This can be used to create a case-insensitive dictionary.
[deleted] ยท 25 points ยท Posted at 17:55:51 on June 18, 2016 ยท (Permalink)
[deleted]
Spire ยท -5 points ยท Posted at 20:40:34 on June 19, 2016 ยท (Permalink)
Lowercase text typically uses one extra bit per alphabetic character, so converting to lowercase rather than uppercase is not recommended on systems with limited RAM, such as embedded systems.
LpSamuelm ยท 6 points ยท Posted at 13:33:21 on June 20, 2016 ยท (Permalink)
Why would you spread these lies?
mrunleaded ยท 10 points ยท Posted at 01:46:25 on June 19, 2016 ยท (Permalink)
Yea this is because in C# strings are immutable.
o11c ยท 2 points ยท Posted at 21:03:12 on June 19, 2016 ยท (Permalink)
Also, both
ToUpperandToLowerwould be wrong, you need theCaseFoldoperation (I'm not sure how to do this in C#). For example, Greek has two different lowercase sigmas (ฯandฯ) which should compare equal when you are ignoring case, but unchanged when converting to NFC (or even NFKC) and to lowercase.Also note that the uppercase of a single character might not be a single character - for example, German lowercase
รgets uppercased asSSThen there's the fact that the comparison depends on locale (the canonical example being the dotless vs dotted
iin Turkish).wllmsaccnt ยท 1 points ยท Posted at 22:30:35 on June 19, 2016 ยท (Permalink)
You can do a culture specific comparison in .NET pretty easy. There are overloads on String.Equals that you can pass in parameters to either compare without culture, with an 'invariant' culture, with the user's current culture, or with a specific culture.
.NET has pretty good support for culture specific text, date, and time comparisons.
ripvannwinkler ยท 2 points ยท Posted at 03:55:36 on June 20, 2016 ยท (Permalink)
Also, if you're using a dictionary as a lookup for a series of handler functions, you can pass instantiate the dictionary like so:
Then, it doesn't matter if you do either:
The lookup is case insenstiive, so there's no need to ever use ToUpper or ToLower before looking up the key.