We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 2 May 2015

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.

Sample Program

package com.javatutorialcorner.javastring;

public class LastOccurenceofString {

public static void main(String[] args) {
String jtc = "Java tutorials corner 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);
}

}

}


Output


The above code produce following output:



Last Occurrence of Java found at index 93

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to find last occurrence of substring inside string Rating: 5 Reviewed By: eHowToNow