GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String indexOf() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java String indexOf() Method

Last Updated : 19 Nov, 2024

In Java, the String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. In this article, we will learn various ways to use indexOf() in Java.

Example:

Below is the simplest way that returns the index of the first occurrence of a specified character in a string, or -1 if the character does not occur.

Java
// Java code to demonstrate the working of String indexOf()
// using indexOf(char ch)

public class Index1 {
  
    public static void main(String args[]) {
      
        String s = new String("Welcome to geeksforgeeks");

        System.out.print("Found g first at position: ");

        System.out.println(s.indexOf('g'));
    }
}

Output
Found g first at position: 11

Explanation:

In the above example, it finds the first occurrence of the character g in the string "Welcome to geeksforgeeks" and prints its index i.e. 11.

Table of Content

  • Different Variants of indexOf() Method in Java
    • indexOf(char ch, int strt) 
    • indexOf(String str) 
    • indexOf(String str, int strt) 

Different Variants of indexOf() Method in Java

There are 3 variants of the indexOf() method are mentioned below:

1. indexOf(char ch, int strt) 

This method returns the index of the first occurrence of the specified character by starting the search at the specified index.

Example:

Java
// Java code to demonstrate the working of String indexOf(char ch, int strt)
public class Index2 {
  
    public static void main(String args[]) {
      
        String s = new String("Welcome to geeksforgeeks");

        System.out.print(
            "Found g after 13th index at position: ");

        System.out.println(s.indexOf('g', 13));
    }
}

Output
Found g after 13th index at position: 19


2. indexOf(String str) 

This method returns the index of the first occurrence of the specified substring in the string.

Example:

Java
// Java code to demonstrate the working of String indexOf(String str)

public class Index3 {
  
    public static void main(String args[]) {
      
        String s = new String("Welcome to geeksforgeeks");

        // Initializing search string
        String s1 = new String("geeks");

        // print the index of initial character of Substring
        System.out.print(
            "Found geeks starting at position: ");
        System.out.print(s.indexOf(s1));
    }
}

Output
Found geeks starting at position: 11


3. indexOf(String str, int strt) 

This method returns the index of the first occurrence of the specified substring by starting the search at the specified index.

Example:

Java
// Java code to demonstrate the working of String indexOf(String str, int strt)
public class Index4 {
  
    public static void main(String args[]) {

        String s = new String("Welcome to geeksforgeeks");

        // Initializing search string
        String s1 = new String("geeks");

        // print the index of initial character
        // of Substring after 14th position
        System.out.print(
            "Found geeks(after 14th index) starting at position: ");
        System.out.print(s.indexOf(s1, 14));
    }
}

Output
Found geeks(after 14th index) starting at position: 19


Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.