1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
protected
void Page_Load( object
sender, EventArgs e) { string
str = "abcqqqqqabchahhabcaaabcssssdbsssabcaadcsssabc" ; string
key = "abc" ; MatchCollection mc; Regex r = new
Regex(key); // 定义一个Regex对象实例 mc = r.Matches(str); List< int > matchposition = new
List< int >(); for
( int
i = 0; i < mc.Count; i++) //在字符串中找到所有匹配 { var
s = mc[i].Value; //将匹配的字符串添在字符串数组中 if
(s == key) { matchposition.Add(mc[i].Index); //记录匹配字符的位置 } } for
( int
j = 0; j < matchposition.Count(); j++) { Response.Write(matchposition[j] + "<br />" ); } } |
原文:http://www.cnblogs.com/xffy1028/p/3665177.html