特殊波斯字符的 C# IndexOf

C# IndexOf in special persian character

在 persian/arabic 字符中,某些字符在其他字符的顶部或底部使用可选,例如 ِ َ ّ ُ.

在我的示例中,如果我使用这个字符,indexOf 找不到我的单词。认为 persian/arabic 是 rtl 语言。

例如:

منّم => م + ن + ّ + م

C#:

"منّم".IndexOf("من");
return -1

javascript:

var index=    ' منّم '.indexOf('من');
console.log(index);

C# 中发生了什么。谁能解释一下?

指定 CompareOptions.Ordinal as an option should work, together with the IndexOf method of CompareInfo.

CompareInfo info = CultureInfo.CurrentCulture.CompareInfo;
string str = "منّم";
Console.WriteLine(info.IndexOf(str, "من", CompareOptions.Ordinal));

输出为 0。

DotNetFiddle如果你想自己试试

您应该了解 .Net 对 compare/match 字符串使用的不同方法。

Best Practices for Using Strings in .NET

Some overloads with default parameters (those that search for a Char in the string instance) perform an ordinal comparison, whereas others (those that search for a string in the string instance) are culture-sensitive. It is difficult to remember which method uses which default value, and easy to confuse the overloads.

String Operations that Use the Invariant Culture 部分对组合字符进行了简短说明。

通过传入 StringComparison.Ordinal as an argument to the overloaded String.IndexOf(),您还可以完成以下操作:

"منّم".IndexOf("من", StringComparison.Ordinal); // returns 0