gpt4 book ai didi

c# - 为什么这个字符串 ("ʿAbdul-Baha' "^^mso :text@de) doesn't start with "?

转载 作者:太空狗 更新时间:2023-10-29 21:54:09 24 4
gpt4 key购买 nike

"\"ʿAbdul-Baha'\"^^mso:text@de".StartsWith("\"") // is false
"\"Abdul-Baha'\"^^mso:text@de".StartsWith("\"") // is true
(int)'ʿ' // is 703`

有没有人能告诉我为什么?

最佳答案

您需要使用函数的第二个参数BeginsWithStringComparison.Ordinal(或 StringComparison.OrdinalIgnoreCase)。这指示函数按字符值进行比较,而不考虑排序时的文化信息。此引用来自下面的 MSDN 链接:

“使用单词排序规则的操作执行文化敏感比较,其中某些非字母数字 Unicode 字符可能具有分配给它们的特殊权重。使用单词排序规则和特定文化的约定,连字符 ("- ") 可能分配给它的权重非常小,因此“coop”和“co-op”会在排序列表中彼此相邻出现。”

这似乎会影响 BeginsWith 的执行方式,具体取决于语言环境/文化(请参阅 OP 帖子上的评论)- 它对某些人有效,但对其他人无效。

在我下面的示例(单元测试)中,我展示了如果将字符串转换为字符数组并查看第一个字符,它实际上是相同的。调用 BeginsWith 函数时,您需要添加 Ordinal 比较以获得相同的结果。

作为引用,我的语言环境是瑞典语。

更多信息: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);
}

关于c# - 为什么这个字符串 ("ʿAbdul-Baha' "^^mso :text@de) doesn't start with "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22400127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com