Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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();     } } 
like image 845
prydain Avatar asked Apr 23 '15 18:04

prydain


People also ask

What does StartsWith return in C#?

In C#, StartsWith() is a string method. This method is used to check whether the beginning of the current string instance matches with a specified string or not. If it matches then it returns the string otherwise false. Using foreach-loop, it is possible to check many strings.

Is StartsWith in C# case sensitive?

The StartsWith method is called several times using case sensitivity, case insensitivity, and different cultures that influence the results of the search.


1 Answers

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.

like image 132
Caramiriel Avatar answered Oct 22 '22 04:10

Caramiriel