w3resource logo


Introduction to Java Programming

Java String: lastIndexOf() Method

Secondary Nav

lastIndexOf() Method

Contents:

public int lastIndexOf(int ch)

The lastIndexOf() method returns the index within this string of the last occurrence of the specified character. For values of ch in the range from 0 to 0xFFFF (inclusive), the index (in Unicode code units) returned is the largest value k such that:

this.charAt(k) == ch

is true. For other values of ch, it is the largest value k such that:

this.codePointAt(k) == ch

is true. In either case, if no such character occurs in this string, then -1 is returned. The String is searched backwards starting at the last character.

Java Platform: Java SE 8

Syntax :

lastIndexOf(int ch)

Parameter:

Name Description Type
ch a character (Unicode code point). int

Return Value : 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.

Return Value Type: int

Example : Java String lastIndexOf(int ch) Method

The following example shows the usage of java String() method.

import java.util.*;
public class StringMethodExample {
  public static void main(String args[]) {
   String str = "w3resource";  		     // index numbering starts form zero
			                     // 1st overloaded method
   int x = str.lastIndexOf('s');
   System.out.println();
   System.out.println("s index position in str from last position: " + x); 
					// 2nd overloaded method
   System.out.println("s index position in str from index position 1 (count from right): " + str.lastIndexOf('s', 2));
					// 3rd overloaded method
   System.out.println("\nw3r index position in str from last position: " + str.lastIndexOf("w3r")); 
					// 4th overloaded method
   System.out.println("w3r index position in str from 3 position (count from right): " + str.lastIndexOf("w3r", 3)); 
 
   System.out.println("\nc index position in str from index last position: " + str.lastIndexOf('c'));
   System.out.println();
  }
}

Output:

s index position in str from last position: 4          
s index position in str from index position 1 (count from right): -1                                          
                                                       
w3r index position in str from last position: 0        
w3r index position in str from 3 position (count from right): 0                                               
                                                       
c index position in str from index last position: 8 

public int lastIndexOf(int ch, int fromIndex)

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. For values of ch in the range from 0 to 0xFFFF (inclusive), the index returned is the largest value k such that:

(this.charAt(k) == ch) && (k <= fromIndex)

is true. For other values of ch, it is the largest value k such that:

(this.codePointAt(k) == ch) && (k <= fromIndex)

All indices are specified in char values (Unicode code units).

Java Platform: Java SE 8

Syntax :

lastIndexOf(int ch, int fromIndex)

Parameter:

Name Description Type
ch a character (Unicode code point). int
fromIndex the index to start the search from. There is no restriction on the value of fromIndex. 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. int

Return Value : 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.

Return Value Type: int

Example : Java String lastIndexOf(int ch, int fromIndex) Method

The following example shows the usage of java String() method.

public class Example {
  public static void main(String args[]) {
    String str = "This is an Example of lastIndexof :";
    System.out.println();
    System.out.println(str);
    System.out.println("lastIndexOf(t, 60) = " + str.lastIndexOf('t', 60));
    System.out.println();
  }
}

Output:

This is an Example of lastIndexof :                      
lastIndexOf(t, 60) = 25

public int lastIndexOf(String str)

Returns the index within this string of the last occurrence of the specified substring.

The returned index is the largest value k for which:

this.startsWith(str, k)

If no such value of k exists, then -1 is returned.

Java Platform: Java SE 8

Syntax :

lastIndexOf(String str)

Parameter:

Name Description Type
str the substring to search for. int

Return Value : the index of the last occurrence of the specified substring, or -1 if there is no such occurrence.

Return Value Type: int

Example : Java String lastIndexOf(String str) Method

The following example shows the usage of java String() method.

public class Example {
  public static void main(String args[]) {
    String str = "This is an example of lastIndexOF, from w3resource.com";
	System.out.println();
    System.out.println(str);
    System.out.println("lastIndexOf(the) = " + str.lastIndexOf("the"));
	System.out.println();
  }
}

Output:

This is an example of lastIndexOF, from w3resource.com 
lastIndexOf(the) = -1 

public int lastIndexOf(int ch, int fromIndex)

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

For values of ch in the range from 0 to 0xFFFF (inclusive), the index returned is the largest value k such that:

(this.charAt(k) == ch) && (k <= fromIndex)

is true. For other values of ch, it is the largest value k such that:

(this.codePointAt(k) == ch) && (k <= fromIndex)

is true. In either case, if no such character occurs in this string at or before position fromIndex, then -1 is returned.

All indices are specified in char values (Unicode code units).

Java Platform: Java SE 8

Syntax :

lastIndexOf(int ch, int fromIndex)

Parameter:

Name Description Type
ch a character (Unicode code point). int
fromIndex the index to start the search from. There is no restriction on the value of fromIndex. 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. int

Return Value : 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.

Return Value Type: int

Example : Java String lastIndexOf(String str, int fromIndex) Method

The following example shows the usage of java String() method.

public class Example {
  public static void main(String args[]) {
    String str = "This is an example of lastIndexOf, from w3resource.com.";
    System.out.println();
    System.out.println(str);
    System.out.println("lastIndexOf(w3r, 50) = " + str.lastIndexOf("w3r", 50));
    System.out.println();
  }
}

Output:

This is an example of lastIndexOf, from w3resource.com.
lastIndexOf(w3r, 50) = 40

Java Code Editor :







Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program