- 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 a Substring from a specified index
Use the indexOf() method to search for a substring from a given position.
Let’s say the following is our string.
String myStr = " pqrstuvwxyzpqrst";
Searching for the substring “pqrs” in the string. We are beginning the index from 3 for our search.
int begnIndex = 3; strLastIndex = myStr.indexOf("pqrs", begnIndex);
Example
public class Demo { public static void main(String[] args) { String myStr = "pqrstuvwxyzpqrst"; int strLastIndex = 0; int begnIndex = 3; System.out.println("String: "+myStr); strLastIndex = myStr.indexOf("pqrs", begnIndex); System.out.println("The index of substring pqrs in the string beginning from index "+begnIndex+" = "+strLastIndex); } }
Output
String: pqrstuvwxyzpqrst The index of substring pqrs in the string beginning from index 3 = 11
- Related Articles
- Java Program for Anagram Substring Search
- Java Program to search for last index of a group of characters
- Python Program for Anagram Substring Search
- C Program for Anagram Substring Search
- How to check index within a string of a specified substring in JSP?
- Java Program to set a range for displaying substring
- Golang Program to check a string starts with a specified substring
- Golang Program to check a string contains a specified substring or not
- Java Program to get a character located at the String's specified index
- Search for a character from a given position in Java
- How can I search a character or substring in java?
- Java Program to locate a substring in a string
- Golang Program to get the index of the substring in a string
- Search index of a character in a string in Java
- Remove from the specified index of a SortedList in C#

Advertisements