获取下一个相同字符的索引 [英] Get index of next same character

查看:43
本文介绍了获取下一个相同字符的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的文件:某事,某事,某事,某事,某事"

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(',')));

推荐答案

抱歉之前的回答,我为你创建了一个函数,试试这个,你需要传递你的字符串、查找字符和出现次数作为参数.这里是一个演示.

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;
}

这篇关于获取下一个相同字符的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆