简单写了个 String str ="acxyefdxycrtsxyf"; String target = "xy"; while (true) { int sta = str.IndexOf(target, startIndex); if (sta == -1) { break; } lst.Add(sta); startIndex = sta + target.Length; }
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace CSDN_Help { class Program { static void Main(string[] args) { string res = "acxyefdxycrtsxyf"; string des = "xy"; char[] TempSpace=new Char[2]; for (int i = 0; i <= res.Length - des.Length; i++) { res.CopyTo(i, TempSpace, 0, des.Length); if (new string(TempSpace).Equals(des)) { Console.WriteLine(i); } } } } } 当然如果要追求算法的效率,可以看看KMP算法。
int[] list = Regex.Matches("acxyefdxycrtsxyf", "xy").OfType<Match>().Select(t => t.Index).ToArray();
String str ="acxyefdxycrtsxyf";
String target = "xy";
while (true)
{
int sta = str.IndexOf(target, startIndex);
if (sta == -1)
{
break;
}
lst.Add(sta);
startIndex = sta + target.Length;
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace CSDN_Help
{
class Program
{
static void Main(string[] args)
{
string res = "acxyefdxycrtsxyf";
string des = "xy";
char[] TempSpace=new Char[2];
for (int i = 0; i <= res.Length - des.Length; i++)
{
res.CopyTo(i, TempSpace, 0, des.Length);
if (new string(TempSpace).Equals(des))
{
Console.WriteLine(i);
}
}
}
}
}
当然如果要追求算法的效率,可以看看KMP算法。