I'm seeing a weird issue with .NET 7 with the string APIs IndexOf and Contains. I'm using C#. To repro:
("Foo" + (char)1618).Contains("Foo")
returns true .... yet
("Foo" + (char)1618).IndexOf("Foo")
returns -1
1618 is a non-spacing Unicode character.
However, when I add a space before the Unicode character like this:
("Foo" + " " + (char)1618).IndexOf("Foo")
It returns 0 as expected.
Any help would be much appreciated.
Dan
This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
While string.IndexOf(string)
is using CurrentCulture
:
This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at the first character position of this instance and continues until the last character position.
Provide StringComparison
parameter so they are equivalent:
Console.WriteLine(("Foo" + (char)1618).Contains("Foo", StringComparison.CurrentCulture)); // False
Console.WriteLine(("Foo" + (char)1618).IndexOf("Foo")); // -1
Or
Console.WriteLine(("Foo" + (char)1618).Contains("Foo")); // True
Console.WriteLine(("Foo" + (char)1618).IndexOf("Foo", StringComparison.Ordinal)); // 0
But the best option is to always pass the StringComparison
parameter for consistency.
Also Behavior changes when comparing strings on .NET 5+ article can be useful.