Reference for Wiring version 0024+. If you have a previous version, use the reference included with your software. If you see any errors or have any comments, let us know.

Class

String

Name

lastIndexOf()

Examples
String s1 = String("string number one"); //  declares s1 with an initial string

void setup() {
  Serial.begin(9600); 
  pinMode(48, OUTPUT);  // turn ON wiring hardware LED
  digitalWrite(48, HIGH);
  
  String s2 = String(10000);  // s2 holds "10000"
  Serial.println("s2 holds: " + s2);
  
  String s3 = String(567000, DEC);  // s3 holds "567000"
  Serial.println("s3 holds: " + s3);
  
  String s4 = "string number four";  // s4 holds "string number four"
  Serial.println("s4 holds: " + s4);
  
  s4 += " plus another part";  // use + operator to add a string
  Serial.println("now s4 holds: " + s4);  // user + operator when printing to serial
  
  char c = s4.charAt(5);  // get character at position 5
  Serial.print("c holds: ");
  Serial.println(c);  // prints 'g'
  
  String s5 = String("string number one");  // s5 holds "string number one"
  
  int cmp = s1.compareTo(s5);  // compare s1 to s5
  if(cmp == 0) {
    Serial.println("s1 and s5 are equal");  // prints equal
  } else {
    Serial.println("s1 and s5 are different");
  }
  
  if(s5.endsWith("one"))  // check if s5 ends with "one"
    Serial.println("s5 ends with "one"");  // prints "s5 ends with "one""
  
  if(s1.equals(s5))  // check if s1 equal to s5
    Serial.println("s1 and s5 are equal");  // prints equal
  
  String s6 = String("string NUMBER one");  // s6 holds "string NUMBER one"
  if(s5.equalsIgnoreCase(s6))  // check if s5 and s6 are equal ignoring case differences
    Serial.println("s6 and s5 are equal ignoring the case");  // prints equal
  
  Serial.print("index of char R pn s6 is: ");
  Serial.println(s6.indexOf('R'), DEC);  // prints 12

  Serial.print("index of char R on s6 from index 13 is: ");
  Serial.println(s6.indexOf('R', 13), DEC);  // prints -1, not found
  
  String s7 = s6.substring(7, 13);
  Serial.println("s7 is: "+s7);  // prints "NUMBER"
  
  Serial.print("index of string "NUMBER" on s6 is: ");  
  Serial.println(s6.indexOf(s7), DEC);  // prints 7

  Serial.print("last index of char 'n' on s6 is: ");  
  Serial.println(s6.lastIndexOf('n'), DEC);  // prints 15
  
  Serial.print("length of s6 is: ");  
  Serial.println(s6.length(), DEC);  // prints 15

  s6.setCharAt(15, 'N');  // set character at index 15 to 'N'
  Serial.println("s6 is: "+s6);  // prints "string NUMBER oNe"
  
  if(s6.startsWith("string"))  // check if s6 starts with "string"
    Serial.println("s6 starts with "string""); // s6 starts with "string" string
    
  Serial.println("s6 to lower case is: "+s6.toLowerCase());  // prints "string number one"
  
  Serial.println("s6 to upper case is: "+s6.toUpperCase());  // prints "STRING NUMBER ONE"
  
  s6.concat(" plus spaces at the end   ");  // concat a string
  Serial.println("s6 is: "+s6);  // prints "string NUMBER oNe plus spaces at the end   "
  
  Serial.println("trim of s6 is: "+s6.trim());  // prints "string NUMBER oNe plus spaces at the end"

  Serial.println("s6 is: "+s6.replace('e', '5'));  // prints "string NUMBER oN5 plus spac5s at th5 5nd"
  
  const char *str = s6.toCharArray();  // get the char array
  Serial.print("str is: ");
  Serial.println(str);  // prints "string NUMBER oNe plus spaces at the end   "
}

void loop() {
  
}
Description Tests to see if a substring or char is embedded in a string and returns the index position of the last occurrence of the substring defined in the str or c parameter. If the str parameter is not found in the string, -1 is returned.
Syntax
lastIndexOf(c)
lastIndexOf(c, fromIndex)
lastIndexOf(str)
lastIndexOf(str, fromIndex)
Parameters
c char: the char to search
str String: the substring to search
fromIndex int: the index from which to start the search
Returns int
Usage Application
Updated on November 01, 2009 01:02:46pm PST

Creative Commons License