/* String indexOf() and lastIndexOf() functions Examples of how to evaluate, look for, and replace characters in a String created 27 Jul 2010 modified 2 Apr 2012 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/StringIndexOf */ void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // send an intro: Serial.println("\n\nString indexOf() and lastIndexOf() functions:"); Serial.println(); } void loop() { // indexOf() returns the position (i.e. index) of a particular character in a // String. For example, if you were parsing HTML tags, you could use it: String stringOne = ""; int firstClosingBracket = stringOne.indexOf('>'); Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket); stringOne = ""; int secondOpeningBracket = firstClosingBracket + 1; int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket); Serial.println("The index of the second > in the string " + stringOne + " is " + secondClosingBracket); // you can also use indexOf() to search for Strings: stringOne = ""; int bodyTag = stringOne.indexOf(""); Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag); stringOne = ""; int firstListItem = stringOne.indexOf("
  • "); int secondListItem = stringOne.indexOf("
  • ", firstListItem + 1); Serial.println("The index of the second list tag in the string " + stringOne + " is " + secondListItem); // lastIndexOf() gives you the last occurrence of a character or string: int lastOpeningBracket = stringOne.lastIndexOf('<'); Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket); int lastListItem = stringOne.lastIndexOf("
  • "); Serial.println("The index of the last list tag in the string " + stringOne + " is " + lastListItem); // lastIndexOf() can also search for a string: stringOne = "

    Lorem ipsum dolor sit amet

    Ipsem

    Quod

    "; int lastParagraph = stringOne.lastIndexOf("