Just for fun. You can achieve your aim with one line of Linq
string target = "ciao";
string str = "ciaobciaodciao";
var indices = str.Select((c, i) => i <= str.Length - target.Length && str.Substring(i, target.Length) == target ? i : -1)
.Where(i => i != -1);
Console.WriteLine($"[{string.Join(", ", indices)}]");
I Benchmarked this method against a Regex equivalent method. The RegexTest was 43% slower and used 38% more memory
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
public class LinqVRegex
{
private string target = "ciao";
private string testString = "ciaobciaodciao";
[Benchmark(Baseline = true)]
public List<int> LinqTest()
{
return testString.Select((c, i) => i <= testString.Length - target.Length && testString.Substring(i, target.Length) == target ? i : -1)
.Where(i => i != -1).ToList();
}
[Benchmark]
public List<int> RegexTest()
{
MatchCollection matches = Regex.Matches(testString, target, RegexOptions.IgnoreCase);
return matches.Select(m => m.Index).ToList();
}
}