Search code examples
c#.netloopsobjectlogic

Skipping a range of values in for loop C#


I'm trying to cycle through chars in a string.

string cycleMe = "Hi StackOverflow! Here is my string."

However, I want to skip over certain ranges of indexes. The ranges I want to skip over are stored in a List of objects, delims.

List<Delim> delims = delimCreator();

To retrieve each starting index and ending index for a range, I have to write a loop that accesses each "delim":

delims[0].getFirstIndex() //results in, say, index 2
delims[0].getLastIndex() //results in, say, index 4

delims[1].getFirstIndex() //results in, say, index 5
delims[1].getLastIndex() //results in, say, index 7

(there can be infinitely many "delim" objects in play)

If the above were my list, I'd want to print the string cycleMe, but skip all the chars between 2 and 4 (inclusive) and 5 and 7 (inclusive).

Expected output using the numbers above:

HiOverflow! Here is my string.

Here is the code I have written so far. It loops far more often than I'd expect (it loops ~x2 the number of characters in the string). Thanks in advance! =)

  List<Delim> delims = delimAggregateInator(displayTextRaw);

  for (int x = 0; x < cycleMe.Length;x++){

    for (int i = 0; i < delims.Count; i++){

      if (!(x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex())){


          Debug.Log("test");


      }
    }

Solution

  • I assume that by skipping you meant you want to omit those characters from the original string. If that is the case, you can try Aggregate extension method like below.

    string result = delims.Aggregate<Delim, string>(cycleMe, (str, d) => cycleMe = cycleMe.Remove(d.FirstIndex, (d.LastIndex - d.FirstIndex) + 1));
    

    Make sure that the delim list is in the proper order.