当使用阿拉伯文提供为什么String.IndexOf和String.Contains不同意?阿拉伯文、不同意、IndexOf、String

2023-09-03 04:58:35 作者:顾九夕

我想知道如果我发现了一个bug在.NET Framework,或者如果我不明白的东西。 运行这块code后:

I want to know if I found a bug in the .NET Framework, or if I don't understand something. After running this piece of code:

var text = "مباركُ وبعض أكثر من نص";
var word = "مبارك";
bool exist = text.Contains(word);
int index = text.IndexOf(word);

的结果是的存在= true和索引= -1

The results are the "exists = true" and "index = -1"

这怎么可能?

推荐答案

包含 是文化不敏感:

Contains is culture-insensitive:

此方法执行序号(区分​​大小写和文化无关)的比较。

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

的IndexOf 是文化敏感的:

IndexOf is culture-sensitive:

此方法执行单词(区分大小写和文化敏感)使用当前区域性的搜索。

This method performs a word (case-sensitive and culture-sensitive) search using the current culture.

这就是区别。如果你使用

That's the difference. If you use

int index = text.IndexOf(word, StringComparison.Ordinal);

然后你会得到0,而不是索引-1(所以它符合包含)。

还有没文化的敏感超载蕴含;目前还不清楚我是否可以使用的IndexOf 可靠这一点,但是的 CompareInfo 类提供的部分的更多的选择。 (我真的不知道很多关于文化的比较,特别是与RTL文本的细节。我只知道它的复杂!)

There's no culture-sensitive overload of Contains; it's unclear to me whether you can use IndexOf reliably for this, but the CompareInfo class gives some more options. (I really don't know much about the details of cultural comparisons, particularly with RTL text. I just know it's complicated!)