I explain, there is a certain list of lines

List<string>Separator = new List<string>(); Separator.Add("0000"); Separator.Add("1111"); Separator.Add("2222"); Separator.Add("3333"); 

And there is a string - _String. in it you need to find all the sequences from the list. Separator wrote such code

 foreach (string str in Separator) { int i = 0; for (; i != -1;) { i = _String.IndexOf(str, i + 4); if (i != -1) SeparatorInt.Add(i); } } 

The program finds all the sequences except the one that will be at the very beginning (start in the first four characters of _String). Tell me how to change the code, so that the program would look in the first four characters?

  • 2
    Replace int i = 0; int i = -4; . - Dmitry D.
  • thanks helped - polsok
  • @polsok, now it's your turn. - Andrei NOP

1 answer 1

The problem is that in the very first iteration of the loop, with i = 0 , the starting position for the search in the second argument of the IndexOf method is 4.

Simply replace int i = 0 with int i = -4 , and the first search will be performed starting at position 0 in the line:

 foreach (string str in Separator) { for (int i = -4; i != -1;) { i = _String.IndexOf(str, i + 4); if (i != -1) SeparatorInt.Add(i); } }