Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if I have this string : "ciaobciaodciao" and I must find the substring "ciao", but if I write:

int index = fileContent.IndexOf("ciao");


index is the first "ciao", but I want to find the index of every "ciao"

What I have tried:

I tried to use the method "IndexOf" like I wrote above, but I need to find every substring
Posted
Updated 12-Apr-23 20:55pm
v2
Comments
PIEBALDconsult 12-Apr-23 11:46am    
Use a Regular Expression.

Using Regular Expressions is very easy.

C#
namespace Examples
{
    public class Example2
    {
        public static void Main()
        {
            string pattern = "ciao";
            string input = "ciao this is a test of ciao and another ciao to go";
            Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
        }
    }
}


output

Match 1: 0-4	ciao
Match 2: 23-27	ciao
Match 3:40-44	ciao
 
Share this answer
 
v3
Comments
George Swan 13-Apr-23 3:37am    
Mike, should that not be
MatchCollection m = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
Once you have called IndexOf, you can then use it again via an overload to return each in sequence - just pass it the index of the most recently located occurrence plus the length of the search string: String.IndexOf Method (System) | Microsoft Learn[^]
 
Share this answer
 

Just for fun. You can achieve your aim with one line of Linq


C#
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)}]");//prints [0, 5, 10]

I Benchmarked this method against a Regex equivalent method. The RegexTest was 43% slower and used 38% more memory


C#
[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();

     }
   }
//|    Method |     Mean |    Error |   StdDev | Ratio | RatioSD |   Gen0 | Allocated | Alloc Ratio |
//|---------- |---------:|---------:|---------:|------:|--------:|-------:|----------:|------------:|
//|  LinqTest | 542.3 ns | 10.86 ns | 17.22 ns |  1.00 |    0.00 | 0.1545 |     648 B |        1.00 |
//| RegexTest | 774.6 ns | 13.60 ns | 26.53 ns |  1.43 |    0.06 | 0.2136 |     896 B |        1.38 |
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900