I\'m wondering what the correct way to compare two characters ignoring case that will work for all cultures. Also, is Comparer
the best way
You can provide last argument as true for caseInsensetive match
string.Compare(lowerCase, upperCase, true);
string.Compare("string a","STRING A",true)
It will work for every string
What I was thinking that would be available within the runtime is something like the following
public class CaseInsensitiveCharComparer : IComparer<char> {
private readonly System.Globalization.CultureInfo ci;
public CaseInsensitiveCharComparer(System.Globalization.CultureInfo ci) {
this.ci = ci;
}
public CaseInsensitiveCharComparer()
: this(System.Globalization.CultureInfo.CurrentCulture) { }
public int Compare(char x, char y) {
return Char.ToUpper(x, ci) - Char.ToUpper(y, ci);
}
}
// Prints 3
Console.WriteLine("This is a test".CountChars('t', new CaseInsensitiveCharComparer()));