Using left.ToUpper() == right.ToUpper()
is not the best option to compare strings, at least because of Performance issues. I want to refactor (fully preserving behavior!) this code, to something efficient, but can't achieve full equivalency for the special case.
So, here is a simple test method:
[TestCase("Strasse", "Straße", "tr-TR")]
[TestCase("İ", "i", "tr-TR")]
public void UsingToUpper_AndCurrentCultureIgnoreCase_AreSame(string left, string right, string culture)
{
// Arrange, Act
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
var toUpper = left.ToUpper() == right.ToUpper();
var stringComparison = left.Equals(right, StringComparison.CurrentCultureIgnoreCase);
// Assert
Assert.AreEqual(toUpper, stringComparison);
}
I tried two options,
StringComparison.CurrentCultureIgnoreCase
and StringComparison.OrdinalIgnoreCase
both of them fails (in different cases).
So, the question:
Is there a way to compare two strings, without changing case and fully preserve the behavior of ToUpper()?