Java String indexOf

by Dan Bunker

 

Java String indexOf and lastIndexOf

 

Sometimes you need to figure out where certain substrings are in a larger string. You can use the String Class methods indexOf(String str) and lastIndexOf(String str) to search the string and return the position of the substring parameter. If you need to parse out characters of a string and then re-splice the larger string back together these methods work good for that along with the substring() method.

  • indexOf(String str) finds the first occurance of the given string and returns its beginning position.
  • lastIndexOf(String str) finds the last occurrance of the given string and returns its beginning position.