Get Second to last character position from string

前端 未结 10 1843
再見小時候
再見小時候 2020-12-19 08:11

I have a dynamically formed string like - part1.abc.part2.abc.part3.abc

In this string I want to know position of second to last \".\" so that i can split string as

10条回答
  •  被撕碎了的回忆
    2020-12-19 08:47

    This would be the solution, with the best possible performance (it probably won't get much faster and memory lightweight than this, unless you want to go the unsafe route):

    public static int LastIndexOf(this string str, char charToSearch, int repeatCound)
    {
        int index = -1;
        for(int i = str.Length - 1; i >= 0, numfound < repeatCound)
        {
            if(str[i] == charToSearch)
            {
                index = i; 
                numfound++;
            }
        }
    
        return index;
    }
    

提交回复
热议问题