String Comparison - Turkish i Problem

Turkish i Problem

Have you heard of The "Turkish i Problem"?

  • Don't use ToUpper()/ToLower() for string comparison in all cultures. Why?
  • Let's imagine we have the word "FunctionHandler", and from the api, we got input ("FUNCTIONHANDLER"). 
  • We want to check if the input and our word are the same.

We use the ToUpper() method:

English version: i.ToUpper() => I

By comparing all letters, we get the result: TRUE

Turkish version: i.ToUpper() => i

I is not equal to i, so the result is FALSE.

public bool FunctionHandler(string input, ILambdaContext context)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
    string turkishWord = "FunctionHandler";
    bool isEqual = input.ToUpper() == "FUNCTIONHANDLER"; // return false
    return isEqual;
}

Solution: Use StringComparison.Original or StringComparison.OriginalIgnoreCase enums to compare by ordinal (binary) sort rules.

public boolFunctionHandler(string input, ILambdaContext context)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
    string turkishWord = "FunctionHandler";
    bool isEqual = string.Equals(input, "FUNCTIONHANDLER", stringComparison.OriginalIgnoreCase); // return true
    return isEqual;
}

Conclusion

Using ToUpper() may return different results for different cultures.