Java Reference Library →

String indexOf() method - Syntax and Examples

String indexOf() method - Syntax and Examples

Learn about the Java String indexOf() method, its syntax, parameters, return value, and how to use it with examples.

This reference explains String indexOf() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

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.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →