Search code examples
c#stringstring-comparisoncultureinfohardcode

Is String culture info important for comparison if the user will never modify those strings


When comparing hard-coded Strings that the User will see but not modify/change, is the culture info important.

I would assume not, but I just want to be safe.

Example:

static void Main()
{
    string hardString = "IAMHardCodei";
    string hardString2 = "IamHardCodei";
    //Compare hardString and hardString2, ignoring case, 
    //and then do stuff based on that result
}

Solution

  • The general recommendation for string comparisons when they are "programmatic only strings" (i.e. as you specified they are not usable or editable by the user directly is the: StringComparison.Ordinal[IgnoreCase].

    See this SO post (of which this is probably a duplicate): Which is generally best to use -- StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?