/*
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.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringIndexOf
*/
特に何もしていません。
setup()
15
16
17
18
19
20
21
22
23
24
25
26
voidsetup(){// 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();}
voidloop(){// 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:
StringstringOne="<HTML><HEAD><BODY>";intfirstClosingBracket=stringOne.indexOf('>');Serial.println("The index of > in the string "+stringOne+" is "+firstClosingBracket);stringOne="<HTML><HEAD><BODY>";intsecondOpeningBracket=firstClosingBracket+1;intsecondClosingBracket=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="<HTML><HEAD><BODY>";intbodyTag=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>";intfirstListItem=stringOne.indexOf("<LI>");intsecondListItem=stringOne.indexOf("<LI>",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:
intlastOpeningBracket=stringOne.lastIndexOf('<');Serial.println("The index of the last < in the string "+stringOne+" is "+lastOpeningBracket);intlastListItem=stringOne.lastIndexOf("<LI>");Serial.println("The index of the last list tag in the string "+stringOne+" is "+lastListItem);// lastIndexOf() can also search for a string:
stringOne="<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";intlastParagraph=stringOne.lastIndexOf("<p");intsecondLastGraf=stringOne.lastIndexOf("<p",lastParagraph-1);Serial.println("The index of the second to last paragraph tag "+stringOne+" is "+secondLastGraf);// do nothing while true:
while(true);}