1

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
}
VMAtm
  • 27,943
  • 17
  • 79
  • 125
JackBarn
  • 635
  • 1
  • 6
  • 18

3 Answers3

1

You can use InvariantCultureIgnoreCase for the comparing

hardString.Equals(hardString2, StringComparison.InvariantCultureIgnoreCase);
VMAtm
  • 27,943
  • 17
  • 79
  • 125
1

Culture rules are relevant even for hardcoded strings if you're relying on the default CurrentCulture when doing comparisons. Here's a textbook example out of the MSDN documentation:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US")
Debug.Assert("file" == "FILE".ToLower()); // Passes

Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
Debug.Assert("file" == "FILE".ToLower()); // Fails

With case insensitive comparison, your users computers in Turkey don't think file and FILE are identical, whereas in the US they are identical.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
0

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?

Community
  • 1
  • 1
CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • @VMAtm It always depends on the very specific implementation needs, that was why I prefixed it with: "The general recommendation". :) – CodingGorilla Apr 25 '15 at 16:14