为什么在提供阿拉伯文本时 String.IndexOf 和 String.Contains 不一致? [英] Why are String.IndexOf and String.Contains disagreeing when provided with Arabic text?

查看:20
本文介绍了为什么在提供阿拉伯文本时 String.IndexOf 和 String.Contains 不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否在 .NET Framework 中发现了一个错误,或者我是否有什么不明白的地方.运行这段代码后:

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);

结果是exists = true"和index = -1"

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

怎么可能?

推荐答案

Contains 不区分区域性:

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 的索引(所以它与 Contains 一致).

then you'll get an index of 0 instead of -1 (so it's consistent with Contains).

Contains 没有对文化敏感的重载;我不清楚您是否可以为此可靠地使用 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!)

这篇关于为什么在提供阿拉伯文本时 String.IndexOf 和 String.Contains 不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆