2010-07-28 00:59:18 +02:00
|
|
|
/*
|
|
|
|
String indexOf() and lastIndexOf() functions
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Examples of how to evaluate, look for, and replace characters in a String
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
created 27 July 2010
|
|
|
|
modified 2 Apr 2012
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/StringIndexOf
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
This example code is in the public domain.
|
2010-07-28 00:59:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void setup() {
|
2012-04-06 12:59:54 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2010-07-28 00:59:18 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-06 20:00:31 +02:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2012-04-06 20:00:31 +02:00
|
|
|
}
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2012-04-06 20:00:31 +02:00
|
|
|
// send an intro:
|
2010-07-28 00:59:18 +02:00
|
|
|
Serial.println("\n\nString indexOf() and lastIndexOf() functions:");
|
2012-04-06 20:00:31 +02:00
|
|
|
Serial.println();
|
2010-07-28 00:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2012-04-06 12:59:54 +02:00
|
|
|
// indexOf() returns the position (i.e. index) of a particular character
|
2017-07-12 19:58:31 +02:00
|
|
|
// in a String. For example, if you were parsing HTML tags, you could use it:
|
2010-07-28 00:59:18 +02:00
|
|
|
String stringOne = "<HTML><HEAD><BODY>";
|
|
|
|
int firstClosingBracket = stringOne.indexOf('>');
|
|
|
|
Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);
|
|
|
|
|
|
|
|
stringOne = "<HTML><HEAD><BODY>";
|
|
|
|
int secondOpeningBracket = firstClosingBracket + 1;
|
2015-07-06 15:18:33 +02:00
|
|
|
int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket);
|
2010-07-28 00:59:18 +02:00
|
|
|
Serial.println("The index of the second > in the string " + stringOne + " is " + secondClosingBracket);
|
|
|
|
|
|
|
|
// you can also use indexOf() to search for Strings:
|
|
|
|
stringOne = "<HTML><HEAD><BODY>";
|
|
|
|
int bodyTag = stringOne.indexOf("<BODY>");
|
|
|
|
Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);
|
|
|
|
|
|
|
|
stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
|
|
|
|
int firstListItem = stringOne.indexOf("<LI>");
|
2015-11-30 13:23:57 +01:00
|
|
|
int secondListItem = stringOne.indexOf("<LI>", firstListItem + 1);
|
|
|
|
Serial.println("The index of the second list tag in the string " + stringOne + " is " + secondListItem);
|
2010-07-28 00:59:18 +02:00
|
|
|
|
|
|
|
// 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("<LI>");
|
2015-11-30 13:23:57 +01:00
|
|
|
Serial.println("The index of the last list tag in the string " + stringOne + " is " + lastListItem);
|
2010-07-28 00:59:18 +02:00
|
|
|
|
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// lastIndexOf() can also search for a string:
|
|
|
|
stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
|
2010-07-28 00:59:18 +02:00
|
|
|
int lastParagraph = stringOne.lastIndexOf("<p");
|
2010-08-11 22:26:17 +02:00
|
|
|
int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
|
2015-11-30 13:23:57 +01:00
|
|
|
Serial.println("The index of the second to last paragraph tag " + stringOne + " is " + secondLastGraf);
|
2010-07-28 00:59:18 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// do nothing while true:
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2012-04-06 12:59:54 +02:00
|
|
|
}
|