[bug / unclear specs] Strings starting with '1' (0x31) are identical to strings starting with '𑁒' (\U00011052) #7876
-
ContextI am developing a small library, in which custom string to double parsing is employed. The parsing function also takes care of "unconventional" unicode symbols, i.e. numbers prefixed with BehaviourI noticed the following (possible) bug when filtering for unicode minus/hyphen signs: string one = 1.ToString();
bool starts = one.StartsWith("\U00011052"); // 𑁒
bool equals_inv = one.Equals("\U00011052", System.StringComparison.InvariantCulture);
bool equals = one.Equals("\U00011052");
System.Console.WriteLine(starts);
System.Console.WriteLine(equals_inv);
System.Console.WriteLine(equals); prints the following output:
(sharplab) Unclear Specs?Now, I get that https://www.compart.com/en/unicode/U+11052 defines the codepoint as "Brahmi Number One", however, I'm not sure why '1' and '𑁒' are considered equal due to their different optical appearance. Furthermore, the documentation on QuestionWould it be possible to clarify this behaviour somehow in the C# docs? I mean either the comparison on non-latin number symbols or the explanation of the following behaviour: string A = "1";
string B = "𑁒";
_ = A.StartsWith(B); // True
_ = A.Equals(B); // False |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The behavior of string operations belongs to dotnet/runtime repo. It's not a part of the language. It is an inconsistency that Meanwhile, you can use globalization analyzers to help you specify behavior explicitly, or simply check the source code at source.dot.net about the option-less overload. Documentation improvement requests should be sent at dotnet/docs. |
Beta Was this translation helpful? Give feedback.
The behavior of string operations belongs to dotnet/runtime repo. It's not a part of the language.
It is an inconsistency that
Equals
defaults to ordinal whileStartsWith
defaults to culture-aware. Unfortunately, this cannot be easily addressed due to the huge compatibility debt for 25 years. There was discussion about helping this at dotnet/runtime#43956.Meanwhile, you can use globalization analyzers to help you specify behavior explicitly, or simply check the source code at source.dot.net about the option-less overload. Documentation improvement requests should be sent at dotnet/docs.