You need to use the second parameter of the BeginsWith function; StringComparison.Ordinal (or StringComparison.OrdinalIgnoreCase ). This instructs the function to compare by symbol value and not take cultural sorting information into account. This quote is from the MSDN link below:
"An operation that uses word sorting rules performs a culture-sensitive match in which some non-Unanumeric Unicode characters can have special weights assigned to them. Using word sorting rules and certain culture conventions, hyphens (" - ") can have very little weight assigned him, so that the "chicken coop" and the "cooperative" appear next to each other in a sorted list. "
This seems to affect how BeginsWith performs depending on the language / culture (see comments on the OP post) - it works for some, but not for others.
In my (unit-test) example below, I show that if you convert strings to char -array and look at the first character, it is actually the same. When calling the BeginsWith function, you need to add an Ordinal comparison to get the same result.
For reference, my language is Swedish.
For more information: MSDN: StringComparison enumeration
[Test] public void BeginsWith_test() { const string string1 = "\"ʿAbdul-Baha'\"^^mso: text@de "; const string string2 = "\"Abdul-Baha'\"^^mso: text@de "; var chars1 = string1.ToCharArray(); var chars2 = string2.ToCharArray(); Assert.That(chars1[0], Is.EqualTo('"')); Assert.That(chars2[0], Is.EqualTo('"')); Assert.That(string1.StartsWith("\"", StringComparison.InvariantCulture), Is.False); Assert.That(string1.StartsWith("\"", StringComparison.CurrentCulture), Is.False); Assert.That(string1.StartsWith("\"", StringComparison.Ordinal), Is.True);
source share