1 回答
TA贡献1801条经验 获得超8个赞
您基本上想在另一个字符串数组中找到一个字符串数组的索引。我们可以根据以下问题改编代码:
public static class Extensions {
public static int UnicodeIndexOf(this string input, string find, StringComparison comparison = StringComparison.CurrentCulture) {
return IndexOf(
// split input by code points
input.ToTextElements().ToArray(),
// split searched value by code points
find.ToTextElements().ToArray(),
comparison);
}
// code from another answer
private static int IndexOf(string[] haystack, string[] needle, StringComparison comparision) {
var len = needle.Length;
var limit = haystack.Length - len;
for (var i = 0; i <= limit; i++) {
var k = 0;
for (; k < len; k++) {
if (!String.Equals(needle[k], haystack[i + k], comparision)) break;
}
if (k == len) return i;
}
return -1;
}
public static IEnumerable<string> ToTextElements(this string input) {
var e = StringInfo.GetTextElementEnumerator(input);
while (e.MoveNext()) {
yield return e.GetTextElement();
}
}
}
- 1 回答
- 0 关注
- 126 浏览
添加回答
举报