发现在C#中的字符串中的所有索引模式 [英] Find all pattern indexes in string in C#

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

问题描述

我如何才能找到一个字符串中使用C#模式的所有索引?



例如我想找到的所有 ## 格局指数在这样的字符串 45 ## 78 $$#$$ 56 JK01UU


解决方案

 字符串模式=##; 
串句=45 ## 78 $$#$$ 56 J ##代K01UU
&IList的LT; INT>的indeces =新的List< INT>();
的foreach(赛的比赛中Regex.Matches(句子,图案))
{
indeces.Add(match.Index);
}



的indeces将有2个,14


How can I find all indexes of a pattern in a string using c#?

For example I want to find all ## pattern indexes in a string like this 45##78$$#56$$JK01UU

解决方案

 string pattern = "##";
 string sentence = "45##78$$#56$$J##K01UU";
 IList<int> indeces = new List<int>();
 foreach (Match match in Regex.Matches(sentence, pattern))
 {
      indeces.Add(match.Index);
 }

indeces will have 2, 14

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

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