- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to search the last position of a substring in Java
Problem Description
How to search the last position of a substring?
Solution
This example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.
public class SearchlastString { public static void main(String[] args) { String strOrig = "Hello world ,Hello Reader"; int lastIndex = strOrig.lastIndexOf("Hello"); if(lastIndex == - 1){ System.out.println("Hello not found"); } else { System.out.println("Last occurrence of Hello is at index "+ lastIndex); } } }
Result
The above code sample will produce the following result.
Last occurrence of Hello is at index 13
Example
This another example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.
public class HelloWorld{ public static void main(String []args) { String t1 = "Tutorialspoint"; int index = t1.lastIndexOf("p"); System.out.println(index); } }
The above code sample will produce the following result.
9
java_strings.htm
Advertisements