Search code examples
c#stringunicodestartswith

Why does string.StartsWith("\u2D2D") always return true?


I was fiddling around with parsing in C# and found that for every string I tried, string.StartsWith("\u2D2D") will return true. Why is that?

It seems it works with every char. Tried this code with .Net 4.5 the Debugger did not break.

for (char i = char.MinValue; i < char.MaxValue; i++)
{
    if(!i.ToString().StartsWith("\u2d2d"))
    {
        Debugger.Break();
    }
}

Solution

  • I think I'll have a try.

    From what I get, is that U+2D2D was added in Unicode v6.1 (source / source).

    The .NET framework, or the native calls rather, support a lower version:

    The culture-sensitive sorting and casing rules used in string comparison depend on the version of the .NET Framework. In the .NET Framework 4.5 running on the Windows 8 operating system, sorting, casing, normalization, and Unicode character information conforms to the Unicode 6.0 standard. On other operating systems, it conforms to the Unicode 5.0 standard. (source)

    Thus it is required to mark it as an ignorable character, which behaves just as if the character wasn't even there.

    Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. (source)

    Example:

    var culture = new CultureInfo("en-US");
    int result = culture.CompareInfo.Compare("", "\u2D2D", CompareOptions.None);
    Assert.AreEqual(0, result);
    

    string.StartsWith uses a similar implementation, but uses CompareInfo.IsPrefix(string, string, CompareOptions) instead.