Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String indexOf() method
Syntax and Examples



Introduction

The indexOf() method in Java is a powerful tool for finding the position of a specific character or substring within a string. Think of it like searching for a word in a book – indexOf() helps you pinpoint exactly where that word starts.

Syntax


public int indexOf(int ch) 
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)

Parameters

Let's break down what each of these variations takes as input:

Parameter Description
ch (int) The character to search for. This is its Unicode value.
ch (int), fromIndex (int) Searches for the character ch starting from the index specified by fromIndex.
str (String) The substring to search for.
str (String), fromIndex (int) Searches for the substring str starting from the index specified by fromIndex.

Return Value

The indexOf() method returns an integer representing the index of the first occurrence of the character or substring within the string. If the character/substring is not found, it returns -1.

Examples

Example 1: Finding a Single Character

This example demonstrates how to find the index of a single character in a string using the simplest form of indexOf(). We'll search for the character 'e' within the string "Hello".


String str = "Hello"; 
int index = str.indexOf('e');
System.out.println(index); // Output: 1
1

The character 'e' appears at index 1 in the string "Hello". Remember that indexing starts from 0.

Example 2: Finding a Character Starting From a Specific Index

Sometimes you want to search for a character only after a certain point in the string. This example uses the second overload of indexOf(), specifying the starting index as 2. We'll search for 'e' in "Hello" again.


String str = "Hello"; 
int index = str.indexOf('e', 2);
System.out.println(index); // Output: 1
1

Since we started the search from index 2, it still finds 'e' at index 1. If 'e' didn't exist after index 2, the output would have been -1.

Example 3: Finding a Substring

This example demonstrates how to find the index of a substring within a string. We'll search for the substring "lo" in the string "Hello".


String str = "Hello"; 
int index = str.indexOf("lo");
System.out.println(index); // Output: 3
3

The substring "lo" starts at index 3 in the string "Hello".

Example 4: Finding a Substring Starting From a Specific Index

This example is similar to Example 3, but we'll specify a starting index of 1. We'll search for "lo" within "Hello" again.


String str = "Hello"; 
int index = str.indexOf("lo", 1);
System.out.println(index); // Output: 3
3

Even though we started searching from index 1, "lo" still starts at index 3.

Example 5: Character Not Found

This example illustrates what happens when the character you're looking for isn't present in the string. We'll search for 'z' within "Hello".


String str = "Hello"; 
int index = str.indexOf('z');
System.out.println(index); // Output: -1
-1

Since 'z' doesn't exist in "Hello", indexOf() returns -1.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M