String.StartsWith 当下一个字符是主符号时不工作(字符)697
String.StartsWith not Working when next character is the Prime Symbol (char)697
我正在尝试使用其中包含 Prime 符号的字符串,但我在使用 String.StartsWith 方法时遇到了一些问题。为什么下面的代码会抛出异常?
string text_1 = @"123456";
string text_2 = @"ʹABCDEF";
string fullText = text_1 + text_2;
if (!fullText.StartsWith(text_1))
{
throw new Exception("Unexplained bad error.");
}
我怀疑问题是因为这个 Prime 符号 (char)697 被视为重音符号,因此正在更改它前面的字母。 (我不认为它应该是 - 它应该是 the prime symbol and so should not be changing the numerical numbers in front of it). I am not exactly sure how to go about testing this. I did try the method proposed in this answer 但它返回 false:
IsLetterWithDiacritics(text_1[5]) // == False
IsLetterWithDiacritics(fullText[5]) // == False
IsLetterWithDiacritics(fullText[6]) // == False
感谢您的帮助。
来自MSDN:
When you call a string comparison method such as String.Compare, String.Equals, or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. For more information, see Best Practices for Using Strings in the .NET Framework.
如果你想进行非语言比较,你应该使用StringComparison.Ordinal
。下面的代码不会抛出异常。
string text_1 = @"123456";
string text_2 = @"ʹABCDEF";
string fullText = text_1 + text_2;
if (!fullText.StartsWith(text_1, StringComparison.Ordinal))
{
throw new Exception("Unexplained bad error.");
}
ʹ
或 MODIFIER LETTER PRIME 是间距修饰符字母。它不是真正的字符,而是修饰前面字符的特殊用途符号。
A modifier letter is a free-standing spacing character that, like a combining character, indicates modifications of a preceding letter.
string.StartsWith
返回 false,因为在您的连接字符串中,6 实际上由附加在其后的素数符号修改。
我正在尝试使用其中包含 Prime 符号的字符串,但我在使用 String.StartsWith 方法时遇到了一些问题。为什么下面的代码会抛出异常?
string text_1 = @"123456";
string text_2 = @"ʹABCDEF";
string fullText = text_1 + text_2;
if (!fullText.StartsWith(text_1))
{
throw new Exception("Unexplained bad error.");
}
我怀疑问题是因为这个 Prime 符号 (char)697 被视为重音符号,因此正在更改它前面的字母。 (我不认为它应该是 - 它应该是 the prime symbol and so should not be changing the numerical numbers in front of it). I am not exactly sure how to go about testing this. I did try the method proposed in this answer 但它返回 false:
IsLetterWithDiacritics(text_1[5]) // == False
IsLetterWithDiacritics(fullText[5]) // == False
IsLetterWithDiacritics(fullText[6]) // == False
感谢您的帮助。
来自MSDN:
When you call a string comparison method such as String.Compare, String.Equals, or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. For more information, see Best Practices for Using Strings in the .NET Framework.
如果你想进行非语言比较,你应该使用StringComparison.Ordinal
。下面的代码不会抛出异常。
string text_1 = @"123456";
string text_2 = @"ʹABCDEF";
string fullText = text_1 + text_2;
if (!fullText.StartsWith(text_1, StringComparison.Ordinal))
{
throw new Exception("Unexplained bad error.");
}
ʹ
或 MODIFIER LETTER PRIME 是间距修饰符字母。它不是真正的字符,而是修饰前面字符的特殊用途符号。
A modifier letter is a free-standing spacing character that, like a combining character, indicates modifications of a preceding letter.
string.StartsWith
返回 false,因为在您的连接字符串中,6 实际上由附加在其后的素数符号修改。