Java - String lastIndexOf() Method



Declaration

Following is the declaration for Java String lastIndexOf() method

public int lastIndexOf(int ch)

Parameters

ch − This is the value of character(Unicode code point).

Return Value

This method returns the index of the last occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

Exception

NA

Java - String lastIndexOf(int ch, int fromIndex) Method

Description

The Java String lastIndexOf(int ch, int fromIndex) method returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

Declaration

Following is the declaration for Java String lastIndexOf() method

public int lastIndexOf(int ch, int fromIndex)

Parameters

  • ch − This is the value of character(Unicode code point).

  • fromIndex − This is the index to start the search from. If it is greater than or equal to the length of this string, it has the same effect as if it were equal to one less than the length of this string: this entire string may be searched. If it is negative, it has the same effect as if it were -1: -1 is returned.

Return Value

This method returns the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

Exception

NA

Java - String lastIndexOf(String str) Method

Description

The java.lang.String.lastIndexOf(String str) method Returns the index within this string of the rightmost occurrence of the specified substring. The rightmost empty string "" is considered to occur at the index value this.length().

The returned index is the largest value k such that this.startsWith(str, k) is true.

Declaration

Following is the declaration for Java String lastIndexOf() method

public int lastIndexOf(String str)

Parameters

str − This is the substring to search for.

Return Value

If the string argument occurs one or more times as a substring within this object, then the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.

Exception

NA

Java - String lastIndexOf(String str, int fromIndex) Method

Description

The Java String lastIndexOf(String str, int fromIndex) method returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. The integer returned is the largest value k such that −

k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k), if no such value of k exists, then -1 is returned.

Declaration

Following is the declaration for Java String lastIndexOf() method

public int lastIndexOf(String str, int fromIndex)

Parameters

  • str − This is the substring to search for.

  • fromIndex − This is the index to start the search from.

Return Value

This method returns the index within this string of the last occurrence of the specified substring.

Exception

NA

Getting Last Index of a Char from a String Example

The following example shows the usage of Java String lastIndexOf() method. We created a string literal with the value "This is tutorialspoint". Then using the lastIndexOf() method, we are retrieving the index Of e.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str = "This is tutorialspoint";
       
      // returns the index of last occurrence of character i
      System.out.println("last index of letter 'i' =  " 
         + str.lastIndexOf('i')); 
      
      // returns -1 as character e is not in the string
      System.out.println("last index of letter 'e' =  " 
         + str.lastIndexOf('e'));
   }
}

Let us compile and run the above program, this will produce the following result −

last index of letter 'i' = 19
last index of letter 'e' = -1

Getting Last Index of a Char from a String with an Index Example

The following example shows the usage of Java String lastIndexOf() method. We created a string literal with the value "This is tutorialspoint". Then using the lastIndexOf() method, we are retrieving the index Of e.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str = "This is tutorialspoint";
   
      /* returns positive value(last occurrence of character t) as character
         is located, which searches character t backward till index 14 */
      System.out.println("last index of letter 't' =  "
         + str.lastIndexOf('t', 14)); 
      
      /* returns -1 as character is not located under the give index,
         which searches character s backward till index 2 */
      System.out.println("last index of letter 's' =  "
         + str.lastIndexOf('s', 2)); 
      
      // returns -1 as character e is not in the string
      System.out.println("last index of letter 'e' =  "
         + str.lastIndexOf('e', 5));
   }
}

Let us compile and run the above program, this will produce the following result −

last index of letter 't' = 10
last index of letter 's' = -1
last index of letter 'e' = -1

Getting Last Index of a String from a String Example

The following example shows the usage of Java String lastIndexOf() method. We created a string literal with the value "Collections of tutorials at tutorials point". Then using the lastIndexOf() method, we are retrieving the index Of tutorials.


package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      // returns index of first character of the last substring "tutorials" 
      System.out.println("index =  " + str1.lastIndexOf("tutorials")); 
      
      // returns -1 as substring "admin" is not located
      System.out.println("index =  " + str1.lastIndexOf("admin"));    
   }
}

Let us compile and run the above program, this will produce the following result −

index = 28
index = -1

Getting Last Index of a String from a String with an Index Example

The following example shows the usage of Java String lastIndexOf() method. We created a string literal with the value "Collections of tutorials at tutorials point". Then using the lastIndexOf() method, we are retrieving the index Of tutorials.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      /* search starts from index 17 and if located it returns 
         the index of the first character of the last substring "tutorials" */
      System.out.println("index = " + str1.lastIndexOf("tutorials", 17)); 

      /* search starts from index 9 and returns -1 as substring "admin"
         is not located */
      System.out.println("index = " + str1.lastIndexOf("admin", 9));     
   }
}

Let us compile and run the above program, this will produce the following result −

index = 15
index = -1
java_lang_string.htm
Advertisements