Java String lastIndexOf()

The lastIndexOf() method in Java is used to find the index of the last occurrence of a specified character or substring within a given string. It starts searching the string from the end and returns the index of the last occurrence of the specified character or substring, or -1 if the character or substring is not found.

The lastIndexOf() method can be called in two ways:

  1. lastIndexOf(int ch): This method takes an integer ch as an argument, which represents the character to be searched. It returns the index of the last occurrence of the specified character in the given string.
  2. lastIndexOf(String str): This method takes a string str as an argument, which represents the substring to be searched. It returns the index of the last occurrence of the specified substring in the given string.

Here is an example of using the lastIndexOf() method:

String str = "Hello, World!";
int index1 = str.lastIndexOf('o'); // returns 8
int index2 = str.lastIndexOf("World"); // returns 7
int index3 = str.lastIndexOf("Java"); // returns -1

In the example above, the lastIndexOf() method is used to find the index of the last occurrence of the character ‘o’, the substring “World”, and the substring “Java” in the given string “Hello, World!”. The lastIndexOf() method returns 8 for ‘o’ because the last occurrence of ‘o’ is at index 8, and returns 7 for “World” because the last occurrence of “World” is at index 7. The lastIndexOf() method returns -1 for “Java” because “Java” is not present in the given string.

Signature of Java String lastIndexOf():

The lastIndexOf() method in Java has two different signatures:

  1. lastIndexOf(int ch): This method takes a single argument of type int, which represents the character to be searched. It returns an int value that represents the index of the last occurrence of the specified character within the string. If the character is not found, the method returns -1.
public int lastIndexOf(int ch)
  1. lastIndexOf(String str): This method takes a single argument of type String, which represents the substring to be searched. It returns an int value that represents the index of the last occurrence of the specified substring within the string. If the substring is not found, the method returns -1.
public int lastIndexOf(String str)

Both of these methods are instance methods of the String class in Java, which means they can only be called on instances of the String class.

Parameters of Java String lastIndexOf():

The lastIndexOf() method in Java takes one parameter, which can be of either int or String type, depending on the signature of the method being used.

  1. lastIndexOf(int ch): This method takes a single parameter of type int, which represents the Unicode value of the character to be searched for in the given string. For example, if we want to find the index of the last occurrence of the letter ‘o’ in a given string, we can call the method as follows:
String str = "Hello, World!";
int lastIndexOfO = str.lastIndexOf(111);

Here, 111 is the Unicode value of the letter ‘o’. The lastIndexOf() method will return the index of the last occurrence of ‘o’ in the given string, which in this case is 8.

  1. lastIndexOf(String str): This method takes a single parameter of type String, which represents the substring to be searched for in the given string. For example, if we want to find the index of the last occurrence of the substring “World” in a given string, we can call the method as follows:
String str = "Hello, World!";
int lastIndexOfWorld = str.lastIndexOf("World");

Here, the lastIndexOf() method will return the index of the last occurrence of the substring “World” in the given string, which in this case is 7. If the given substring is not found in the given string, the method will return -1.

In both cases, the lastIndexOf() method starts searching the string from the end and returns the index of the last occurrence of the specified character or substring. If the specified character or substring is not found in the given string, the method returns -1.

Returns of Java String lastIndexOf():

The lastIndexOf() method in Java returns an int value that represents the index of the last occurrence of the specified character or substring within the given string. If the specified character or substring is not found in the given string, the method returns -1.

The return value of the lastIndexOf() method indicates the position of the last occurrence of the specified character or substring within the string. The position is an integer value that represents the index of the character or substring within the string, with the first character or substring being at index 0.

For example, if we want to find the index of the last occurrence of the letter ‘o’ in a given string, we can call the lastIndexOf() method as follows:

String str = "Hello, World!";
int lastIndexOfO = str.lastIndexOf('o');

In this case, the lastIndexOf() method will return the index of the last occurrence of ‘o’ in the given string, which is 8. If the specified character or substring is not found in the given string, the method returns -1.

Similarly, if we want to find the index of the last occurrence of the substring “World” in a given string, we can call the lastIndexOf() method as follows:

String str = "Hello, World!";
int lastIndexOfWorld = str.lastIndexOf("World");

In this case, the lastIndexOf() method will return the index of the last occurrence of the substring “World” in the given string, which is 7. If the specified character or substring is not found in the given string, the method returns -1.

Internal Implementation:

The internal implementation of the lastIndexOf() method in Java depends on the specific version of the Java runtime environment being used, but in general it uses an algorithm similar to a linear search from the end of the string.

When the method is called with a single int argument, the implementation starts searching the string from the end, iterating over the characters of the string from right to left, until it finds the specified character or reaches the beginning of the string. If the specified character is found, the method returns the index of that character within the string. If the specified character is not found, the method returns -1.

When the method is called with a String argument, the implementation first checks if the length of the search string is greater than the length of the source string. If this is the case, the method immediately returns -1, since the search string cannot possibly occur within the source string. Otherwise, the implementation starts searching the source string from the end, iterating over the characters of the string from right to left, until it finds the last occurrence of the search string or reaches the beginning of the string. If the search string is found, the method returns the index of the first character of the search string within the source string. If the search string is not found, the method returns -1.

The lastIndexOf() method has a time complexity of O(n), where n is the length of the string being searched. However, the method can be optimized to terminate early if the search string is not found, which can improve performance for large strings.

Java String lastIndexOf() method example:

Sure, here’s an example of using the lastIndexOf() method in Java:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Find the index of the last occurrence of 'o'
        int lastIndex = str.lastIndexOf('o');
        System.out.println("Last index of 'o': " + lastIndex);

        // Find the index of the last occurrence of "the"
        int lastIndexOfThe = str.lastIndexOf("the");
        System.out.println("Last index of \"the\": " + lastIndexOfThe);
    }
}

In this example, we first create a String object containing the sentence “The quick brown fox jumps over the lazy dog.”.

We then use the lastIndexOf() method to find the index of the last occurrence of the letter ‘o’ within the string, and store the result in the variable lastIndex. The output of the program shows that the last index of ‘o’ within the string is 40.

Next, we use the lastIndexOf() method to find the index of the last occurrence of the substring “the” within the string, and store the result in the variable lastIndexOfThe. The output of the program shows that the last index of “the” within the string is 31.

Note that both calls to lastIndexOf() in this example start searching the string from the end, since we want to find the last occurrence of the specified character or substring.

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

Sure, here’s an example of using the lastIndexOf(int ch, int fromIndex) method in Java:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Find the index of the last occurrence of 'o' starting from index 30
        int lastIndex = str.lastIndexOf('o', 30);
        System.out.println("Last index of 'o' starting from index 30: " + lastIndex);

        // Find the index of the last occurrence of 'x' starting from index 25
        int lastIndexOfX = str.lastIndexOf('x', 25);
        System.out.println("Last index of 'x' starting from index 25: " + lastIndexOfX);
    }
}

In this example, we first create a String object containing the sentence “The quick brown fox jumps over the lazy dog.”.

We then use the lastIndexOf(int ch, int fromIndex) method to find the index of the last occurrence of the letter ‘o’ within the string, starting from index 30, and store the result in the variable lastIndex. The output of the program shows that the last index of ‘o’ within the string, starting from index 30, is 26.

Next, we use the lastIndexOf(int ch, int fromIndex) method to find the index of the last occurrence of the letter ‘x’ within the string, starting from index 25, and store the result in the variable lastIndexOfX. The output of the program shows that the last index of ‘x’ within the string, starting from index 25, is -1. This is because there is no occurrence of ‘x’ within the string, starting from index 25.

Note that both calls to lastIndexOf(int ch, int fromIndex) in this example start searching the string from the specified index, since we are searching for the last occurrence of the specified character after the specified index.

Java String lastIndexOf(String substring) Method Example:

Sure, here’s an example of using the lastIndexOf(String substring) method in Java:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Find the index of the last occurrence of "the"
        int lastIndexOfThe = str.lastIndexOf("the");
        System.out.println("Last index of \"the\": " + lastIndexOfThe);

        // Find the index of the last occurrence of "dog" in the first 20 characters
        int lastIndexOfDog = str.lastIndexOf("dog", 20);
        System.out.println("Last index of \"dog\" in the first 20 characters: " + lastIndexOfDog);
    }
}

In this example, we first create a String object containing the sentence “The quick brown fox jumps over the lazy dog.”.

We then use the lastIndexOf(String substring) method to find the index of the last occurrence of the substring “the” within the string, and store the result in the variable lastIndexOfThe. The output of the program shows that the last index of “the” within the string is 31.

Next, we use the lastIndexOf(String substring) method to find the index of the last occurrence of the substring “dog” within the first 20 characters of the string, and store the result in the variable lastIndexOfDog. The output of the program shows that there is no occurrence of “dog” within the first 20 characters of the string, so the method returns -1.

Note that the first call to lastIndexOf(String substring) in this example starts searching the string from the end, since we want to find the last occurrence of the specified substring. The second call to lastIndexOf(String substring) specifies a starting index of 20, so the search is restricted to the first 20 characters of the string.

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

Sure, here’s an example of using the lastIndexOf(String substring, int fromIndex) method in Java:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Find the index of the last occurrence of "he" before index 15
        int lastIndexOfHe = str.lastIndexOf("he", 15);
        System.out.println("Last index of \"he\" before index 15: " + lastIndexOfHe);

        // Find the index of the last occurrence of "the" before index 30
        int lastIndexOfThe = str.lastIndexOf("the", 30);
        System.out.println("Last index of \"the\" before index 30: " + lastIndexOfThe);
    }
}

In this example, we first create a String object containing the sentence “The quick brown fox jumps over the lazy dog.”.

We then use the lastIndexOf(String substring, int fromIndex) method to find the index of the last occurrence of the substring “he” within the string, before index 15, and store the result in the variable lastIndexOfHe. The output of the program shows that the last index of “he” within the string, before index 15, is 9.

Next, we use the lastIndexOf(String substring, int fromIndex) method to find the index of the last occurrence of the substring “the” within the string, before index 30, and store the result in the variable lastIndexOfThe. The output of the program shows that the last index of “the” within the string, before index 30, is 16.

Note that both calls to lastIndexOf(String substring, int fromIndex) in this example start searching the string from the specified index, since we want to find the last occurrence of the specified substring before the specified index. The first call starts searching from index 15 and stops at index 0, while the second call starts searching from index 30 and stops at index 0 as well.