String indexOf() and lastIndexOf() Method
Look for the first/last instance of a character in a string.
The String object indexOf() method gives you the ability to search for the first instance of a particular character value in a String. You can also look for the first instance of the character after a given offset. The lastIndexOf() method lets you do the same things from the end of a String.
1String stringOne = "<HTML><HEAD><BODY>";2int firstClosingBracket = stringOne.indexOf('>');In this case,
firstClosingBracket>firstClosingBracket + 11stringOne = "<HTML><HEAD><BODY>";2int secondClosingBracket = stringOne.indexOf('>', firstClosingBracket + 1 );The result would be 11, the position of the closing bracket for the HEAD tag.
If you want to search from the end of the String, you can use the
lastIndexOf()1stringOne = "<HTML><HEAD><BODY>";2int lastOpeningBracket = stringOne.lastIndexOf('<');In this case,
lastOpeningBracket<stringOne.lastIndexOf('<', lastOpeningBracket -1)Hardware Required
- Arduino Board
Circuit
There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.
Code
Learn more
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language.
Last revision 2015/08/11 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.
