- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Java Program to search for last index of a group of characters
Use the lastIndexOf() method to search for last index of a group of characters. Let’s say the following is our string.
String myStr = "pqrstuvwxyzpqrst";
Searching for the substring “pqrs” in the string to get the last index of its occurring in the string. We begin the search from index 3.
int begnIndex = 3; strLastIndex = myStr.indexOf("pqrs", begnIndex);
The following is an example, wherein we are searching for the last index of the substring “pqrs”
Example
public class Demo { public static void main(String[] args) { String myStr = "pqrstuvwxyzpqrst"; int strLastIndex = 0; System.out.println("String: "+myStr); strLastIndex = myStr.lastIndexOf("pqrs"); System.out.println("The last index of substring pqrs in the string "+myStr+" = "+strLastIndex); } }
Output
String: pqrstuvwxyzpqrst The last index of substring pqrs in the string pqrstuvwxyzpqrst = 11
- Related Articles
- Java Program to search for a Substring from a specified index
- Java program to swap first and last characters of words in a sentence
- MySQL query to search within the last 5 characters in a column?
- Search index of a character in a string in Java
- C# program to remove a range of characters by index using StringBuilder
- Python Program to Remove the Characters of Odd Index Values in a String
- Java Program for Anagram Substring Search
- Java Program for Binary Search (Recursive)
- Java Program to Swap Pair of Characters
- Program to find number of minimum steps to reach last index in Python
- Java program to convert a list of characters into a string
- Search for specific characters within a string with MySQL?
- Loop through an index of an array to search for a certain letter in JavaScript
- Get the last index of a particular element in an ArrayList in Java
- Golang Program to get characters from a string using the index

Advertisements