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

1

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?

Frendel answered 14/3, 2014 at 8:51 Comment(7)
Both are true. Your character ʿ is not at the first but the second position, so how is it related?Koetke
I just tried.. both are trueFinish
What locale are you using for your comparison?Thomasina
My guess is a Mono bug...Circuity
InvariantCulture Return False and True. "\"ʿAbdul-Baha'\"^^mso:text@de".StartsWith("\"",StringComparison.InvariantCulture ); // is false "\"Abdul-Baha'\"^^mso:text@de".StartsWith("\"",StringComparison.InvariantCulture ); // is trueChronon
@Circuity i tried with mono 2.10.2 (tarball Wed Sep 25 16:35:44 CDT 2013) working fineFinish
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.Koetke
E
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);
}
Ephemeral answered 14/3, 2014 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.