nth出现在字符串中的od字符 [英] nth occurence od character in a string

查看:65
本文介绍了nth出现在字符串中的od字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用LINQ的情况下找到字符串中第n个字符的出现

How to find nth occurence of a character in a string without using LINQ

推荐答案

你有没有尝试过?



这将是一个简单的for循环if。类似于:

Did you try anything?

It would be a simple for loop with an if. Something like:
public int GetNthOccurenceIndex(string sample, char t, int nth)
{
    int count = 0;
    for (int i = 0; i < sample.Length; i++)
    {
        if (sample[i] == t)
        {
            count++;
            if (count == nth)
               return i;
        }
    }
    // -1 means nth occurrence was not found.
    return -1;
}


MatchCollection mc = Regex.Matches(strList,c.ToString());

if(mc .Count> = numCount)

result = mc [numCount - 1] .Index;
MatchCollection mc = Regex.Matches(strList, c.ToString());
if (mc.Count >= numCount)
result = mc[numCount - 1].Index;


这篇关于nth出现在字符串中的od字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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