Get index of next same character [duplicate]

给你一囗甜甜゛ 提交于 2019-12-13 08:38:47

问题


I have a file like this: "Something, something, something, something, something"

And I want to get the index of third "," with:

StreamReader sr=new StreamReader("File.txt");
string s=sr.Readline();    
string c=s.Substring(0, s.IndexOf(',')));

回答1:


Sorry for previous answer, I created a function for you, try this, You need to pass your string, character to find and occurance number as parameters. Here is a demo.

public static int getSpecifiedIndexOf(string str,char ch,int index)
{
    int i = 0,o=1;
    while ((i = str.IndexOf(ch, i)) != -1)
    {
        if(o==index)return i;
        o++;
        i++;
    }
    return 0;
}



回答2:


Just specify which character do you want and which occurance in next parameter

StreamReader sr=new StreamReader("File.txt");
string s=sr.Readline();    
string c=s.Substring(0, s.IndexOf(',',3)));

Here is your solution



来源:https://stackoverflow.com/questions/23633861/get-index-of-next-same-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!