C# - 的indexOf一个字符串的第n次出现? [英] C# - indexOf the nth occurrence of a string?

查看:663
本文介绍了C# - 的indexOf一个字符串的第n次出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非我缺少一个明显的内置方法是什么让一个字符串中的字符串的第n次出现的最快方式。

Unless I am missing an obvious built in method what is the quickest way to get the nth occurrence of a string within a string.

我可以循环中的indexOf方法开始索引更新每个循环,但它的声音浪费在我身上。

I could loop the indexof method with the start index being updated on each loop but it sounds wasteful to me.

推荐答案

这基本上是你需要做的 - 至少,这是最简单的解决方案。所有你会被浪费是N方法调用的成本 - 你不会实际检查任何情况下的两倍,如果你考虑一下吧。 (的IndexOf会尽快找到匹配的回报,你会继续从离开的地方去。)

That's basically what you need to do - or at least, it's the easiest solution. All you'd be "wasting" is the cost of n method invocations - you won't actually be checking any case twice, if you think about it. (IndexOf will return as soon as it finds the match, and you'll keep going from where it left off.)

这篇关于C# - 的indexOf一个字符串的第n次出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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