Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this string ("ʿAbdul-Baha'"^^mso:text@de) doesn't start with "?

"\"ʿAbdul-Baha'\"^^mso:text@de".StartsWith("\"") // is false
"\"Abdul-Baha'\"^^mso:text@de".StartsWith("\"") // is true
(int)'ʿ' // is 703`

is there anyone could tell me Why?

like image 896
user3419037 Avatar asked Mar 14 '14 08:03

user3419037


1 Answers

You need to use the second parameter of the function BeginsWith; StringComparison.Ordinal (or StringComparison.OrdinalIgnoreCase). This instructs the function to compare by character value and to take no consideration to cultural information on sorting. This quote is from the MSDN-link below:

"An operation that uses word sort rules performs a culture-sensitive comparison wherein certain nonalphanumeric Unicode characters might have special weights assigned to them. Using word sort rules and the conventions of a specific culture, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list."

This seems to affect how BeginsWith performs depending on locale/culture (see the comments on OP's post) - it works for some but not for others.

In my example (unit-test) below I show that if you convert the strings to a char-array and look at the first character, it it actually the same. When calling the BeginsWith-function you need to add the Ordinal comparison to get the same result.

For reference my locale is Swedish.

For further info: 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); // Works
    Assert.That(string2.StartsWith("\""), Is.True);
}
like image 164
Johan Tegman Avatar answered Nov 05 '22 04:11

Johan Tegman