×

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

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Java Math random() Method

The random() method of Math class returns a double value with a positive sign, less than 1 and greater than or equal to 0.0. This method is properly synchronized with...

1 minute read.

Virtual Function in Java

In the Operated Oriented Programming language, a virtual function or virtual method is a collection of functions that overrides the functionality of a function in an inheriting class with the same...

4 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Integer decode() method

The decode() method of Integer class decodes a String into an Integer. It can accept decimal, hexadecimal and octal numbers. Syntax public static Integer decode(String nm) throws NumberFormatException Parameters The parameter ‘nm’ represents the...

2 minutes read.

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.