Find Text

Little function that searches for a given String within another String. If the search String is found its position is returned, otherwise -1 is returned.

Code:


int find_text(String needle, String haystack) {
  int foundpos = -1;
  for (int i = 0; i <= haystack.length() - needle.length(); i++) {
    if (haystack.substring(i,needle.length()+i) == needle) {
      foundpos = i;
    }
  }
  return foundpos;
}

2016: Code adjusted according this forum post : http://forum.arduino.cc/index.php?topic=394718.0

Share