Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#string.IndexOfNthFromEnd()扩展方法_C#_String_Indexing_Extension Methods - Fatal编程技术网

C#string.IndexOfNthFromEnd()扩展方法

C#string.IndexOfNthFromEnd()扩展方法,c#,string,indexing,extension-methods,C#,String,Indexing,Extension Methods,我找到了一个很好的扩展方法,可以在一些输入字符串中找到第n个出现的值: 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 wit

我找到了一个很好的扩展方法,可以在一些
输入
字符串中找到第n个出现的

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);
}
它应该从字符串的末尾找到第n次出现的索引。例如:

IndexOfNthFromEnd("-!-!----", "!", 2) // = 1;
IndexOfNthFromEnd("-!-!----", "!", 1) // = 3;
IndexOfNthFromEnd("-!-!----", "-!", 2) // = 0;
IndexOfNthFromEnd("-!-!----", "-!", 1) // = 2;

也许不是最优雅的解决方案,但您可以使用以下方法反转输入字符串:

    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    (new string( charArray )).IndexOfNth("-!-!----", "!", 2);
并使用相同的扩展方法

public static int IndexOfNthFromEnd(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");
    var idx = input.LastIndexOf(value, startIndex, StringComparison.Ordinal);
    if (idx == -1 || nth == 1)
        return idx;
    return input.IndexOfNthFromEnd(value, idx - value.Length, --nth);
}

对于:
“abababa”。IndexOfNth(“bab”,0,2)
它返回3,当它应该是-1时,因为
bab
只在字符串中出现一次。

下面是一种迭代方法,保持问题中概述的签名(即,没有起始索引参数)。如果字符串包含大量重复,那么这可能不是最有效的方法,但它可以工作。我还没有从原始例程中添加任何可爱的参数验证

public static int IndexOfNthFromEnd(此字符串输入,字符串值,int n)
{
var newString=输入;
var指数=0;
而(n-->0&&index>=0)
{
index=newString.LastIndexOf(值);
newString=newString.Substring(0,索引);
}
收益指数;
}

试试这个,请看看这个

private static int IndexOfNth(string str,char c,int n){
int s=-1;
对于(int i=0;i
是的,这一点很好,我想您也必须反转该值。就像我说的,不优雅!
public static int IndexOfNthFromEnd(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");
    var idx = input.LastIndexOf(value, startIndex, StringComparison.Ordinal);
    if (idx == -1 || nth == 1)
        return idx;
    return input.IndexOfNthFromEnd(value, idx - value.Length, --nth);
}
return input.IndexOfNth(value, idx + value.Length, --nth);
private static int IndexOfNth(string str, char c, int n) {
    int s = -1;

    for (int i = 0; i < n; i++) {
        s = str.IndexOf(c, s + 1);

        if (s == -1) break;
    }

    return s;
}