Energia

Examples > Strings

### String IndexOf() and lastIndexOf() Methods

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.

### Relevant Groundwork

      String stringOne = "<HTML><HEAD><BODY>";
      int firstClosingBracket = stringOne.indexOf('>');

In this case, firstClosingBracket equals 5, because the first > character is at position 5 in the String (counting the first character as 0). If you want to get the second closing bracket, you can use the fact that you know the position of the first one, and search from firstClosingBracket + 1 as the offset, like so:

      stringOne = "<HTML><HEAD><BODY>";
      int 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() method instead. This function returns the position of the last occurrence of a given character.

      stringOne = "<HTML><HEAD><BODY>";
      int lastOpeningBracket = stringOne.lastIndexOf('<');

In this case, lastOpeningBracket equals 12, the position of the < for the BODY tag. If you want the opening bracket for the HEAD tag, it would be at stringOne.lastIndexOf('<', lastOpeningBracket -1), or 6.

### Hardware Required

- MSP-EXP430G2 LaunchPad

### Circuit

Only LaunchPad is required for this example.

image developed using Fritzing. For more circuit examples, see the Fritzing project page.

### Schematic

### Code Explanation

In the program, we use string index to look for and replace characters in a string.

### Code

    /*
      String indexOf() and lastIndexOf() functions

     Examples of how to evaluate, look for, and replace characters in a String

     created 27 July 2010
     modified 2 Apr 2012
     by Tom Igoe

     This example code is in the public domain.
     */

    void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);

      // 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 = "<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;
      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 = "<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>");
      int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
      Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);

      // 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>");
      Serial.println("The index of the last list item 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>";
      int lastParagraph = stringOne.lastIndexOf("<p");
      int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
      Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);

      // do nothing while true:
      while(true);
    }

### Working Video

(Insert Video Here)

### Try it out:

- Turn a present tense sentance into past tense using index functions

See Also

- setup()
- loop()
- println()

Getting Started Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Energia reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.

©Energia | Edit Page | Page History | Printable View | All Recent Site Changes