我发现,发现的value
第N次出现到一些input
串一个不错的扩展方法:C#string.IndexOfNthFromEnd()扩展方法
public static int IndexOfNth(this string input, string value, int startIndex, int nth)
{
if (nth < 1)
throw new ArgumentException("Can not find the zeroth index of substring in string. Must start with 1");
if (nth == 1)
return input.IndexOf(value, startIndex, StringComparison.Ordinal);
var idx = input.IndexOf(value, startIndex, StringComparison.Ordinal);
if (idx == -1)
return -1;
return input.IndexOfNth(value, idx + value.Length, --nth);
}
我需要下一个,但不能改变它自己:
IndexOfNthFromEnd(this string input, string value, int nth)
它应该从字符串结尾找到第N个出现的索引。例如:
IndexOfNthFromEnd("-!-!----", "!", 2) // = 1;
IndexOfNthFromEnd("-!-!----", "!", 1) // = 3;
IndexOfNthFromEnd("-!-!----", "-!", 2) // = 0;
IndexOfNthFromEnd("-!-!----", "-!", 1) // = 2;
是的,你是赖特!谢谢! – AsValeO