🎓 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
In this guide, you will learn about the String indexOf() method in Java programming and how to use it with an example.
1. String indexOf() Method Overview
Definition:
The indexOf() method returns the position of the first occurrence of a specified character(s) in a string.
Syntax:
1. str.indexOf(int ch)
2. str.indexOf(int ch, int fromIndex)
3. str.indexOf(String substring)
4. str.indexOf(String substring, int fromIndex)
Parameters:
- ch: a character (Unicode code point).
- fromIndex: the index to start the search from.
- substring: a string to be searched.
Key Points:
- Returns the index of the first occurrence of the character or substring in the string.
- Returns -1 if the character or substring is not found.
- The search starts at the specified fromIndex and moves towards the end of this string.
- The method throws no exception if fromIndex is negative or greater than this string's length; it just returns -1.
2. String indexOf() Method Example
public class IndexOfExample {
public static void main(String[] args) {
String sample = "JavaProgramming";
// Searching for a character
int index1 = sample.indexOf('a');
System.out.println("First occurrence of 'a': " + index1);
// Searching from a specific index
int index2 = sample.indexOf('a', 2);
System.out.println("Occurrence of 'a' after index 2: " + index2);
// Searching for a substring
int index3 = sample.indexOf("Prog");
System.out.println("First occurrence of 'Prog': " + index3);
// Searching for a non-existent substring
int index4 = sample.indexOf("Hello");
System.out.println("Searching for 'Hello': " + index4);
}
}
Output:
First occurrence of 'a': 1 Occurrence of 'a' after index 2: 3 First occurrence of 'Prog': 4 Searching for 'Hello': -1
Explanation:
In the example:
1. We first search for the character 'a' in the string "JavaProgramming". It returns the index of the first occurrence.
2. Next, we search for the character 'a' starting from index 2. It finds the next occurrence after that index.
3. Then, we search for the substring "Prog" and it returns the index of its first occurrence.
4. Lastly, we search for a non-existent substring "Hello". Since it's not in the string, -1 is returned.
Related Java String Class method examples
- Java String charAt() example
- Java String concat() example
- Java String contains() example
- Java String endsWith() example
- Java String equals() example
- Java String equalsIgnoreCase() example
- Java String getBytes() example
- Java String indexOf() example
- Java String isEmpty() example
- Java String lastIndexOf() example
- Java String length() example
- Java String replace() example
- Java String split() example
- Java String startsWith() example
- Java String substring() example
- Java String toLowerCase() example
- Java String toUpperCase() example
- Java String trim() example
- Java String valueOf() example
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