Which Arduino board / ATmel processor are you using?
where data is the incoming character. This creates a String of typically about 200 characters.
Keep in mind that when a String object is concatenated, a new buffer the size of the original string plus 1 is created. Then the new result is copied into the original buffer. This means you are using 2x the storage required during the operation. So if you have 200 characters and add one, you at that moment, using 400 bytes.
I did some tests and found that using indexOf() on Strings of more than about 160 characters, will fail, returning -1.
How did you generate these 160 characters? Was this by concatenating strings until you got 160 characters to search? If so, you might be running out of RAM which means the String isn't actually storing what you expected which is why indexOf is failing.
Also can anyone suggest perhaps a better approach to the problem? Thanks!
If you know the maximum number of characters you are going to deal with, then just use a character array.