Java string lastindexOf Method

Java String Substring Method

What is a string in Java?

In Java, a string object is an object that represents a sequence of characters. A string object is created using the String class, which is a built-in class in Java. Strings are used to represent text data in Java. 

  • The Java platform provides a rich set of classes and methods for working with strings, making it easy to perform common tasks such as searching, parsing, and formatting strings.

To know more about String Class in java read the complete article.

String lastindexOf() method.

The lastIndexOf() method is a method of the java.lang.String class and is used to find the last occurrence of a specific character or a substring in a given string. It 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 is case-sensitive, so it will not find a match if the character or substring you are searching for does not match the case of the characters in the string.

Syntax:

public int lastIndexOf(int ch,int fromIndex)

Parameters:

ch - a character (Unicode code point).

Let’s look at a string-related Java program where the Java String LastindexOf() Method is used to perform an operation on the given string.

Example: Java String lastindexOf() Method

Run

public class Main {
  public static void main(String[] args) {
    String myStr = "PrepInsta Prime has over 200 courses.";
    System.out.println(myStr.lastIndexOf("courses"));
  }
}

Output

29

Example 2 : Java String  lastindexOf() Method

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "hello prepinsta.";
    System.out.println(myStr.lastIndexOf("h", 5));
  }
}

Output

0

Example 3: how to use the lastindexOf property:

Run

public class Main {
public static void main(String[] args) {
    String str = "Hello World";

    // Extract a substring starting at index 6 and ending at index 11
    String subString = str.substring(6, 11);

    // Print the length of the substring
    System.out.println("Length: " + subString.length());  // Outputs "Length: 5"

    // Check if the substring starts with a specific string
    System.out.println("Starts with 'W': " + subString.startsWith("W"));  // Outputs "Starts with 'W': true"

    // Check if the substring ends with a specific string
    System.out.println("Ends with 'd': " + subString.endsWith("d"));  // Outputs "Ends with 'd': true"

    // Get the character at a specific index in the substring
    char c = subString.charAt(2);
    System.out.println("Char at index 2: " + c);  // Outputs "Char at index 2: r"

    // Convert the substring to uppercase
    String upperCase = subString.toUpperCase();
    System.out.println("Uppercase: " + upperCase);  // Outputs "Uppercase: WORLD"
}
}

Output

Length: 5
Starts with 'W': true
Ends with 'd': true
Char at index 2: r
Uppercase: WORLD

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription