- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display the position of a Substring in Java
To display the position of a Substring in Java, we use the lastindexOf() method in Java. The lastindexOf() method returns the rightmost occurrence of the substring within a particular StringBuffer object.
If the substring occurs more than once in the StringBuffer object, the index of the first character of the rightmost occured substring is returned. If the substring is not found, the lastIndexOf() method returns -1.
Declaration − The java.lang.StringBuffer.lastIndexOf() method is declared as follows−
public int lastIndexOf(String s)
where s is the substring whose index needs to be found
Let us see a program which displays the position of a Substring.
Example
public class Example { public static void main(String[] args) { StringBuffer sb= new StringBuffer("Welcome to Java .Version Java 7"); int pos = sb.lastIndexOf("Java"); System.out.println(pos); } }
Output
25
- Related Articles
- In MySQL, how can we insert a substring at the specified position in a string?
- Count occurrences of a substring recursively in Java
- Sort search results based on substring position in MySQL
- How to display a button in random screen position?
- Get the substring after the first occurrence of a separator in Java
- Get the substring before the last occurrence of a separator in Java
- How to extract substring from a sting starting at a particular position in MySQL?
- MySQL query to display a substring before a special character in a string
- Extracting a substring as an array of characters in Java
- Display the package name of a class in Java
- Display substring in MySQL if the string is less than a specific length or display a custom message if it is more?
- Java Substring Comparisons
- Java Program to locate a substring in a string
- Display the limits of DataTypes in Java
- Searching characters and substring in a String in Java

Advertisements