×

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String.

Methods Description

int indexOf(int ch)

 

 

It returns index position for the given char value.
int indexOf(int ch, int fromIndex)

 

 

It returns index position for the given char value and from index.
int indexOf(String substring)

 

 

It returns index position for the given substring.
int indexOf(String substring, int fromIndex)

 

 

It returns index position for the given substring and from index.

Parameters:

ch: a single character

fromIndex: index position from where index of the char value or substring is returned

substring: substring to be searched in this string

Return:

It returns index of the character within a String

Java String IndexOf() method Example 1

public class JavaStringIndexOfEx1{ 
public static void main(String args[]){ 
String s1="welcome to tutorial and example"; 
//passing substring 
int index1=s1.indexOf("to"); 
int index2=s1.indexOf("and"); 
System.out.println(index1+"  "+index2);
//passing substring with from index 
int index3=s1.indexOf("to",9);
int index4=s1.indexOf("to",5);
System.out.println(index3);
System.out.println(index4);
//passing char value 
int index5=s1.indexOf('a'); 
int index6=s1.indexOf('z'); 
System.out.println(index5); 
System.out.println(index6); 
  }
}

Output:

 8   20 13 8 17 -1

Java String indexOf(String substring) Method Example 2

public class JavaStringIndexOfEx2 { 
    public static void main(String[] args) { 
        String s1 = "India is a great ";
        String s2 = s1.concat("Country in the world");
        // Passing Substring   
        int index = s2.indexOf("the"); //Returns the index of this substring 
         int index2 = s1.indexOf("the");
         int index3 = s2.indexOf("a");
         int index4 = s2.indexOf(" ");
        System.out.println("index of substring "+index);
        System.out.println("index of substring "+index2);
        System.out.println("index of substring "+index3);
        System.out.println("index of substring "+index4);
    } 
}

Output:

index of substring 28
index of substring -1
index of substring 4
index of substring 5

Java String indexOf(String substring, int fromIndex) Method Example 3

public class JavaStringIndexOfEx3 { 
    public static void main(String[] args) { 
        String s1 = "India is a great ";
        String s2 = s1.concat("Country in the world");
        // Passing substring and index    
        int index = s2.indexOf("the", 10); //Returns the index of this substring 
         int index2 = s1.indexOf("the", 5);
         int index3 = s2.indexOf("a", 25);
         //Passing substring
         int index4 = s2.indexOf(" ");
        System.out.println("index of substring "+index);//Returns the index of this substring 
        System.out.println("index of substring "+index2);
        System.out.println("index of substring "+index3);// It returns -1 if substring does not found 
        System.out.println("index of substring "+index4);
    } 
  }

Output:

index of substring 28
index of substring -1
index of substring -1
index of substring 5

Related Topics

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Lazy loading in Java

Lazy loading is the idea of waiting to load an object until you need it. In other words, it is the practice of postponing class instantiation until it is necessary....

5 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.