This is duplicative. Perhaps iterate to find the index of the first occurrence, defaulting to 0, and then have a separate loop to assemble the snippet.