Identifying indices of non-delimited characters in a string, supporting greater than 1 length of delimiters The code below takes a string that potentially contains a true match for a delimiter. There can be multiple delimiters located anywhere in the string. We need to obtain the indices that are not a match and when the delimiter starts and ends.
The following code achieves this. However, I am unsure if this is the most optimal method. What do you think? Test cases below the code.
String baseText = &quot;%!ABCD%!EF%&quot;;
String delimiter = &quot;%&quot;;
ArrayList&lt;String&gt; selectedParameters = new ArrayList&lt;&gt;();


int lastNonDelimited = 0;
int i = 0;
while (i &lt; baseText.length()) {
    int j = 0;
    while (j &lt; delimiter.length()) {

        if (j + i == baseText.length() - 1 &amp;&amp; j != delimiter.length() - 1) {
            lastNonDelimited += 1;
            System.out.println(lastNonDelimited);
            break;
        }

        char c = baseText.charAt(i + j);
        char d = delimiter.charAt(j);

        if (c == d) {
            if (j == delimiter.length() - 1) {
                lastNonDelimited += delimiter.length();
                i += (delimiter.length() - 1);
                System.out.println(&quot;End&quot; + (i + j));
            } else {

                System.out.println(&quot;Beginnning&quot; + (i + j + 1));
            }
        } else {
            lastNonDelimited += 1;
            System.out.println(lastNonDelimited);
            break;
        }

        j += 1;
    }

    i += 1;
}

Test case 1:
String %!ABCD%!EF% with %! delimiter results in: 
Beginnning1
End2
3
4
5
6
Beginnning7
End8
9
10
11

Test case 2:
String %!ABCD%!EF%! with %! delimiter results in:
Beginnning1
End2
3
4
5
6
Beginnning7
End8
9
10
Beginnning11
End12
Some off the cuff comments:
It would help if you provided a compilable class rather than a somewhat misleading snippet.
Your search would be better as a separate method.
I don't see why you search in this way when String.indexof(String) and String.substring(int) exist.
Have you a clear understanding of how you would handle overlapping delimiters? If your delimiter is &quot;!!&quot;, how is &quot;abc!!!def&quot; to be interpreted?