🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
String.indexOf() method in Java is used to find the index of a specified character or substring within a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
indexOfMethod Syntax- Examples
- Finding a Character's Index
- Finding a Substring's Index
- Starting Search from a Specific Index
- Handling Not Found Cases
- Conclusion
Introduction
The String.indexOf() method is a member of the String class in Java. It allows you to locate the position of a character or substring within a string. This method is particularly useful for searching, parsing, and manipulating strings based on specific characters or patterns.
indexOf Method Syntax
The indexOf method has several overloads:
- Finding the index of a character:
public int indexOf(int ch)
- Finding the index of a character starting from a specified index:
public int indexOf(int ch, int fromIndex)
- Finding the index of a substring:
public int indexOf(String str)
- Finding the index of a substring starting from a specified index:
public int indexOf(String str, int fromIndex)
Examples
Finding a Character's Index
The indexOf method can be used to find the first occurrence of a specified character within a string.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int indexOfH = message.indexOf('H');
int indexOfW = message.indexOf('W');
System.out.println("Index of 'H': " + indexOfH);
System.out.println("Index of 'W': " + indexOfW);
}
}
Output:
Index of 'H': 0
Index of 'W': 7
Finding a Substring's Index
The indexOf method can be used to find the first occurrence of a specified substring within a string.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int indexOfHello = message.indexOf("Hello");
int indexOfWorld = message.indexOf("World");
System.out.println("Index of 'Hello': " + indexOfHello);
System.out.println("Index of 'World': " + indexOfWorld);
}
}
Output:
Index of 'Hello': 0
Index of 'World': 7
Starting Search from a Specific Index
The indexOf method can be used to start the search from a specified index.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World! Hello, Java!";
int indexOfHelloFirst = message.indexOf("Hello");
int indexOfHelloSecond = message.indexOf("Hello", 10);
System.out.println("Index of first 'Hello': " + indexOfHelloFirst);
System.out.println("Index of second 'Hello': " + indexOfHelloSecond);
}
}
Output:
Index of first 'Hello': 0
Index of second 'Hello': 14
Handling Not Found Cases
The indexOf method returns -1 if the specified character or substring is not found.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int indexOfJava = message.indexOf("Java");
System.out.println("Index of 'Java': " + indexOfJava);
}
}
Output:
Index of 'Java': -1
Conclusion
The String.indexOf() method in Java for locating the position of characters and substrings within a string. By understanding how to use this method, you can efficiently search, parse, and manipulate strings in your Java applications. Whether you are finding the index of a character, a substring, starting the search from a specific index, or handling cases where the character or substring is not found, the indexOf method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment