6

The following code works on "everyones" machine, except for mine:

var foo = "caa"; var bar = "ca"; if (foo.StartsWith(bar, false, CultureInfo.GetCultureInfo("no"))) Console.WriteLine($"{foo} starts with {bar}"); else { Console.WriteLine($"{foo} does not start with {bar}"); } 

I've tried in on .net 5.0.400, 5.0.403, 6.0.100 - all prints 'caa does not start with ca'. I've not copied the chars from anywhere, I literally type them out.

I understand that this is really just on my computer, but I do not understand why or what is causing this.

Edit: I'm on Windows 10 - OS Build: 19043.1348. nb-NO keyboard, en-US Windows UI language.

Edit: Added if (foo.StartsWith(bar, false, CultureInfo.GetCultureInfo("no") - no change in behavior.

7
  • 4
    It doesn't. The data may not be what you assume. There may be non-printable characters. The characters may look the same but actually refer to different ones. Or you may be from Denmark, where aa is treated as an accented a Commented Nov 25, 2021 at 12:14
  • 1
    What server OS, what's the culture of the machine? See learn.microsoft.com/en-us/dotnet/standard/base-types/…, some internationalization changes were made to string comparisons in .NET 5. Commented Nov 25, 2021 at 12:14
  • 1
    Have you tried with foo.StartsWith(bar, StringComparison.Ordinal)? Commented Nov 25, 2021 at 12:15
  • 1
    Aaaaah, Norway. So what does aa equal to in Norway? Commented Nov 25, 2021 at 12:15
  • 1
    if (foo.StartsWith(bar, false, CultureInfo.GetCultureInfo("no"))) "caa does not start with ca" Commented Nov 25, 2021 at 12:17

1 Answer 1

8

Repro:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("nb-NO"); var foo = "caa"; var bar = "ca"; if (foo.StartsWith(bar)) { Console.WriteLine($"{foo} starts with {bar}"); } else { Console.WriteLine($"{foo} does not start with {bar}"); } 

Since .NET 5, string comparison is done using different string comparison libraries (ICU). In Norwegian, "aa" apparently isn't the same as "a" + "a". See Behavior changes when comparing strings on .NET 5+.

Compare (thanks @Charlieface):

You'll want an ordinal (character numeric value) comparison:

foo.StartsWith(bar, StringComparison.Ordinal) 
Sign up to request clarification or add additional context in comments.

5 Comments

Ooh, interesting, learned something new today. Won't that make life terribly difficult if you have, for example, compound nouns where the first word ends with "a" and the second one starts with "a"?
@Heinzi I'm by no means a linguistics expert, at most a knowingly incompetent enthousiast. I cannot fathom why "aa" is normalized to "å" for comparisons and thereby doesn't end with "a", but for example in German (de-DE) "Strasse".EndsWith("ße") returns false...
Oh, "Strasse".EndsWith("ße") does return true, even in en-US, but only in .NET < 5. Globalization is hard...
@Heinzi here is another example of globalization pitfalls on .NET5+: stackoverflow.com/questions/69314093/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.