How to find last occurrence of substring inside string

Following example we are going to see about “how to find the last occurrence position of substring inside string using lastIndexOf(String str) method in Java.

LastOccurenceofString.Java

package com.ehowtonow.javastring;

public class LastOccurenceofString {

    public static void main(String[] args) {
        String jtc = "eHowToNow Java tutorials contains tutorials for Java programers day to day needs tutorials like Java, Spring, Hibernate, String, JSON, Jackson, GSON, XML parsing, Web Service, SOAP Web Service, RESTful Web Service";
        String subString = "Java";
        int index = jtc.lastIndexOf(subString);
        if (index == -1) {
            System.out.println(subString + " not found");
        } else {
            System.out.println("Last Occurence of " + subString
                    + " found at index " + index);
        }

    }

}

Result

Last Occurrence of Java found at index 93


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java String codePointAt() method with example
  2. Java String charAt() method with example
  3. Java String format() method with example
  4. Remove the particular character from string
  5. String Concatenation in Java
  6. How to format the String by using Locale
  7. Java String equals() method with example
  8. Java String indexOf(String str, int fromIndex) method with example
  9. Java String compareToIgnoreCase() method with example
  10. How to Split the String in Java
  11. Java String copyValueOf() method with example
  12. Java String copyValueOf(char data[]) method with example
  13. Java String indexOf(String str) method with example
  14. Java String indexOf(int ch) method with example
  15. How to get Character unicode point in a String

Be the first to comment

Leave a Reply