BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String indexOf() Method

Last Updated: July 1, 2024 by Chaitanya Singh | Filed Under: java

Java String indexOf() method is used to find the index of a specified character or a substring in a given String. Here are the different variations of this method in String class:

Finding the index of a character:

int indexOf(int ch)

It returns the index of first occurrence of character ch in the given string. If the character is not found, it returns -1.

String str = "Hello, world!";
int index = str.indexOf('o'); // returns 4

Finding the index of a character starting from a specific position:

int indexOf(int ch, int fromIndex)

It returns the index of first occurrence of character ch in the given string, starting the search from specified index fromIndex. If the character is not found, it returns -1.

String str = "Hello, world!";
int index = str.indexOf('o', 5); // returns 8

Finding the index of a substring:

int indexOf(String str)

It returns the index of the first occurrence of the specified substring. If the substring is not found, it returns -1.

String str = "Hello, world!";
int index = str.indexOf("world"); // returns 7

Finding the index of a substring starting from a specific position:

int indexOf(String str, int fromIndex)

It returns the index of string str in the given string after the specified index “fromIndex”. If the substring is not found, it returns -1.

String str = "Hello, world!";
int index = str.indexOf("world", 8); // returns -1

Example 1:

public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Hello, world!";

// Find the index of a character
int index1 = str.indexOf('o');
System.out.println("Index of 'o': " + index1); // Output: 4

// Find the index of a character starting from the given index
int index2 = str.indexOf('o', 5);
System.out.println("Index of 'o' from position 5: " + index2); // Output: 8

// Find the index of a substring
int index3 = str.indexOf("world");
System.out.println("Index of \"world\": " + index3); // Output: 7

// Find the index of a substring starting from the given index
int index4 = str.indexOf("world", 8);
System.out.println("Index of \"world\" from position 8: " + index4); // Output: -1
}
}

Example 2:

Let’s take a simple example with a short string where we are finding the indexes of given chars and substring using the indexOf() method.

public class JavaExample {  
   public static void main(String[] args) {  
	String str = "Java String";
	char ch = 'J';
	char ch2 = 'S';
	String subStr = "tri";
	int posOfJ = str.indexOf(ch);
	int posOfS = str.indexOf(ch2);
	int posOfSubstr = str.indexOf(subStr);
	System.out.println(posOfJ);
	System.out.println(posOfS);
	System.out.println(posOfSubstr);
   }  
}

Output:

0
5
6
❮ Java String Class

Top Related Articles:

  1. Java String startsWith() Method with examples
  2. StringJoiner toString() Method in Java
  3. Java StringBuilder ensureCapacity()
  4. Java StringBuffer lastIndexOf()
  5. Java StringBuffer replace()

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap