6
"\"ʿ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?

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • Both are `true`. Your character `ʿ` is not at the first but the second position, so how is it related? – Tim Schmelter Mar 14 '14 at 08:56
  • I just tried.. both are true – Dexters Mar 14 '14 at 08:58
  • What locale are you using for your comparison? – Rowland Shaw Mar 14 '14 at 08:59
  • My guess is a Mono bug... – leppie Mar 14 '14 at 09:03
  • InvariantCulture Return False and True. "\"ʿAbdul-Baha'\"^^mso:text@de".StartsWith("\"",StringComparison.InvariantCulture ); // is false "\"Abdul-Baha'\"^^mso:text@de".StartsWith("\"",StringComparison.InvariantCulture ); // is true – bit Mar 14 '14 at 09:04
  • @leppie i tried with mono 2.10.2 (tarball Wed Sep 25 16:35:44 CDT 2013) working fine – Dexters Mar 14 '14 at 09:07
  • There's no culture which works, it seems that really the second character `ʿ` is resposible. Checked with: `var workingCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Where(c => "\"ʿ".StartsWith("\"", false, c)).Select(c => c.ToString());`. If you remove that char all cultures work. – Tim Schmelter Mar 14 '14 at 09:21

1 Answers1

2

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);
}
Johan Tegman
  • 102
  • 4