List<int> Positions = new List<int>(); string stringData = "aaaaasaaaasaaaasaaa"; int begin = 0; int current = 0; while(true) { current = stringData.IndexOf('s', begin); if (current != -1) { Positions.Add(current); begin = current + 1; } else { break; } } Console.WriteLine(Positions);
List<int> Positions = new List<int>();
string stringData = "aaaaasaaaasaaaasaaa";
int begin = 0;
int current = 0;
while(true)
{
current = stringData.IndexOf('s', begin);
if (current != -1)
{
Positions.Add(current);
begin = current + 1;
}
else
{
break;
}
} Console.WriteLine(Positions);
{
string 测试文本 = "aaaaasaaaasaaaasaaa";
string 要查找的字符串 = "s";
MatchCollection 捕获集合 = Regex.Matches(测试文本, Regex.Escape(要查找的字符串));
Console.WriteLine("查找的字符为:" + 要查找的字符串 + "一共捕获到 " + 捕获集合.Count.ToString() + "个匹配");
foreach (Match 单个捕获 in 捕获集合)
{
Console.WriteLine("位置:" + 单个捕获.Index);
}
}结果
查找的字符为:s一共捕获到 3个匹配
位置:5
位置:10
位置:15
{
Console.WriteLine(v.i);
} /*
5
10
15
*/