I know that the following is case sensitive:
if (StringA == StringB) {
So is there an operator which will compare two strings in an insensitive manner?
I know that the following is case sensitive:
if (StringA == StringB) {
So is there an operator which will compare two strings in an insensitive manner?
Try this:
string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
System.Collections.CaseInsensitiveComparer
or
System.StringComparer.OrdinalIgnoreCase
Operator? NO, but I think you can change your culture so that string comparison is not case-sensitive.
// you'll want to change this...
System.Threading.Thread.CurrentThread.CurrentCulture
// and you'll want to custimize this
System.Globalization.CultureInfo.CompareInfo
I'm confident that it will change the way that strings are being compared by the equals operator.
string.Equals(StringA, StringB, StringComparison.CurrentCultureIgnoreCase);
if (StringA.ToUpperInvariant() == StringB.ToUpperInvariant()) {
People report ToUpperInvariant() is faster than ToLowerInvariant().
You can use
if (stringA.equals(StringB, StringComparison.CurrentCultureIgnoreCase))
or
if (StringA.Equals(StringB, StringComparison.CurrentCultureIgnoreCase)) {
but you need to be sure that StringA is not null. So probably better tu use:
string.Equals(StringA , StringB, StringComparison.CurrentCultureIgnoreCase);
as John suggested
EDIT: corrected the bug
There are a number of properties on the StringComparer
static class that return comparers for any type of case-sensitivity you might want:
For instance, you can call
StringComparer.CurrentCultureIgnoreCase.Equals(string1, string2)
or
StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)
It's a bit cleaner than the string.Equals
or string.Compare
overloads that take a StringComparison
argument.
System.StringComparer.OrdinalIgnoreCase
http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html