Sunday 13, Apr 2025
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

Related Posts:

  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to make read-only file using Java Following example shows how to make file read-only using file.setReadOnly() method in Java. setReadOnlypublic boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations a… Read More
  • How to reverse the Collection (List) Following example shows how to reverse the collection (List) using Collections.reverse(arg0); method in Java. void java.util.Collections.reverse(List<?> list)reversepublic static void reverse(List<?> list) Rever… Read More
  • How to Split the String in JavaFollowing Example shows how to split the string using str.split(String pattern) method in Java. Sample Programpackage com.javatutorialcorner.javastring;public class SplitString { public static void main(String[] args) { … Read More
  • 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