Fork me on Gitee

利用正则来查找字符串中第n个匹配字符索引

1.string.IndexOf()方法可以获得第一个匹配项的索引

2.要获取第n个匹配项的索引:
 方法1:利用IndexOf方法循环获取。
方法2:用正则来查找。
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(input, query);
通过:matches[count].Index获取

 

其中: input:表示待查找的字符串,
 
  1. query":匹配字符串,
             count":第几个匹配项,从0开始。
 
 
 
posted @ 2019-11-22 18:19  VueDi  阅读(1624)  评论(0编辑  收藏  举报