lastIndexOf String and Character in Java Example

What is lastIndexOf()? What does it do?


lastIndexOf() is a method of the java.lang.String class that gets the last index of a String or a character in a specified string.This is an overloaded method and has been done so for four times. Here are the four perspectives of the overloaded forms of the method.

  public int lastIndexOf(int char);
  public int lastIndexOf(int char, int fromIndex);
  public int lastIndexOf(String text);
  public int lastIndexOf(String text, int fromIndex);


lastIndexOf(int char): This method returns an integer value nothing but the last index of the given char in the String through which this method was called.

lastIndexOf(int char, int fromIndex): This method too returns an integer value nothing but the last index of the given char from the specified index i.e. here, fromIndex means that the last but index. If you couldn't understand, looking the example helps.

lastIndexOf(String text): This method too does the same thing returns an integer value nothing but the index of the specified string text in the string in which this method was called.

lastIndexOf(String text, int fromIndex): This method, returns an int value nothing but the index of the specified string text but from the last but index as said earlier, if you are unable to understand don't worry, looking at the example helps.


The Example


class lastIndexOfString
{
public static void main(String args[])
{

/* For Strings */

// Get the last index of "sample"
System.out.println("Last index of string 'sample' is "+"I am sample sample text.".lastIndexOf("sample"));

// Get the last index of "sample" from 7
System.out.println("Last index of string 'sample' from 7 is "+"I am sample sample text.".lastIndexOf("sample",7));

/* For Single Character */

// Get the last index of "e"
System.out.println("Last index of character 'e' is "+"This is sample text.".lastIndexOf('e'));
// Get the last index of "e" from 14
System.out.println("Last index of character 'e' is "+"This is sample text.".lastIndexOf('e',14));
}
}


-------------------------------------
Output
-------------------------------------

Last index of string 'sample' is 12
Last index of string 'sample' from 7 is 5
Last index of character 'e' is 16
Last index of character 'e' is 13

Explanation


lastIndexOf("sample"): This will return 12 because as there are two sample words in the string, the last sample word's index is what that will be returned. In indexOf, the scene is quite reverse, it returns the index of first String.

lastIndexOf("sample",7): This will return 5 because, i am saying the method to search for the string sample from the index 7 towards left (backwards). For a better understand let us look at this.

"I am sample sample text." here,

Index of I is 0
Index of " " is 1
Index of a is 2
Index of m is 3
Index of " " is 4
Index of s is 5
Index of a is 6
Index of m is 7
Index of is 8
Index of is 9
Index of is 10
and so on.

Here i said to search sample from index 7, then it will go back and search the word sample. Let us analyse this.

Is sample found at 7 : no
Is sample found at 6 : no
Is sample found at 5 : yes

lastIndexOf('e'): The same way, as lastIndexOf("sample"), but it is just a single character. The last e will be returned.

lastIndexOf('e',14): The same way, as lastIndexOf("sample",7), but here it is just a single character and that too from the position 14. 

Hope you understand, if you don't feel free to drop a comment and i will reply to it as soon as possible.