- 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 locate a substring in a string
To locate a substring in a string, use the indexOf() method.
Let’s say the following is our string.
String str = "testdemo";
Find a substring ‘demo’ in a string and get the index.
int index = str.indexOf( 'demo');
Example
public class Demo { public static void main(String []args) { String str = "testdemo"; System.out.println("String: "+str); int index = str.indexOf("demo"); System.out.printf("Substring 'demo' is at index %d
", index); } }
Output
String: testdemo Substring 'demo' is at index 4
- Related Articles
- Java Program to locate a character in a string
- Java Program to Check if a string contains a substring
- Java program to insert a String into a Substring of StringBuffer
- Java program to check if a Substring is present in a given String
- Golang program to check if a string contains a substring
- Golang Program to get a substring from the string
- Searching characters and substring in a String in Java
- Golang Program to check a string starts with a specified substring
- Python Program to check if a substring is present in a given string.
- C# program to check if a Substring is present in a Given String
- How to extract certain substring from a string using Java?
- Golang Program to check a string contains a specified substring or not
- Java Program to set a range for displaying substring
- PHP to check substring in a string
- Golang Program to get the index of the substring in a string

Advertisements