CodeGym /Java Blog /Strings in Java /Java String lastIndexOf() Method
Author
Pavlo Plynko
Java Developer at CodeGym

Java String lastIndexOf() Method

Published in the Strings in Java group
The lastIndexOf() method returns the position of the last occurrence of a specified character or a substring in a string. Imagine that you have some kind of long text, or rather a long line. It can be, for example, a letter, and you need to find the place where the last call to the addressee takes place by the name that you already know. For such cases, the indexOf method of the Java String class is very suitable. If you need the first occurrence of a character in a string, you can use the indexOf() method, it’s very similar to lastIndexOf(). There are four variants of the lastIndexOf() method. Having four methods with the same name but different parameters is possible due to method overloading. Below we will look at all four variations of this method with examples.

lastIndexOf(int ch)

This method returns the index of the last occurrence of the character in the character sequence.

Syntax of the method


int lastIndexOf(int ch)
Parameter: ch: a character.

Code example


public class LastIndexOf1 {

       public static void main(String args[])
       {
          //letter to find index in String
           char letter = 'd';
           //String to find an index of a letter
           String myString = "This is major Tom to ground control, do you copy";
           //The index of last appearance of d will be printed
           System.out.println("Last Index of d = " + myString.lastIndexOf(letter));
       }
}
The output is:
Last Index of d = 37
If the character we are looking for is not in our string, the method returns -1:

public class LastIndexOf1 {

       public static void main(String args[])
       {
           char letter = 'z';
           String myString = "This is major Tom to ground control, do you copy";
           System.out.println("Last Index of z = " + myString.lastIndexOf(letter));
       }
}
The output is:
Last Index of z = -1

lastIndexOf​(int ch, int fromIndex)

lastIndexOf(int ch, int fromIndex): If this character is represented in the string, this method returns the index of the last occurrence of the ch character, searching backward starting at the specified index. if this character isn’t represented in substring, it returns -1.

Syntax of the method


public int lastIndexOf(int ch, int fromIndex)
Parameters: ch: a character. fromIndex: the index to start the search from.

Code examples of lastIndexOf(int ch, int fromIndex)


public class LastIndexOf2 {

   public static void main(String args[])
   {
       //letter to find index in String
       char letter = 'o';
       //String to find an index of a letter
       String myString = "This is major Tom to ground control, do you copy";
       //The index of last appearance of o before 20th symbol will be printed
       System.out.println("Last Index of o = " + myString.lastIndexOf(letter, 20));
   }
}
The output is:
Last Index of o = 19
If, when passing from the index to the beginning of the line, the character is not encountered, the method will return -1:

public class LastIndexOf2 {

   public static void main(String args[])
   {
       char letter = 'o';
       String myString = "This is major Tom to ground control, do you copy";
       System.out.println("Last Index of o = " + myString.lastIndexOf(letter, 10));
   }
}
The output is:
Last Index of o = -1

lastIndexOf(String str)

lastIndexOf(String str): this variation of the method accepts a String as an argument and returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, the method returns -1.

Syntax of the method


public int lastIndexOf(String str)
Parameters: str: a string.

Code examples of lastIndexOf(String str)


public class LastIndexOf3 {
   public static void main(String args[])
   {      
       String myString = "This is major Tom to ground control, do you copy";
       System.out.println( myString.lastIndexOf("Tom"));
   }
}
The output is:
14
If there is no such substring, the method returns -1. Let’s try to find an index of the beginning of the substring “tom”.

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

       String myString = "This is major Tom to ground control, do you copy";
       System.out.println( myString.lastIndexOf("tom"));
   }
}
Remember, that “T” and ‘t” are different symbols, so there is no “tom” in this string. Here is the output:
-1

lastIndexOf(String str, int fromIndex)

lastIndexOf(String str, int fromIndex). This variant of the method returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

Syntax of the method


public int lastIndexOf(String str, int beg)
Parameters str: a string. fromIndex: the index to start the search from.

Code examples of lastIndexOf(String str, int fromIndex)

Let's try to find the index of the last occurrence of the substring “ro” in the string “This is major Tom to ground control, do you copy”. The first time we will go through the entire string, the second time we start from character with index 25 (as we remember, with the upper constraint, the search for the index goes from the end to the beginning).

public class LastIndexOf4 {
   public static void main(String[] args) {
       String myString = "This is major Tom to ground control, do you copy";
       System.out.println( myString.lastIndexOf("ro"));
       System.out.println(myString.lastIndexOf("ro",25));
   }
}
The output is:
32 22
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION