Java - String indexOf() Method



The Java String indexOf() method is, used to retrieve the index value within this string of the first occurrence of the specified character. The index refers to the position of the character in the string value. The index range is 0th index to length()-1 index.

The first char denotes the 0th index value, the second char denotes the 1st index value, and so on. The indexOf() method accepts different parameters.

The indexOf() method has four polymorphic variants with different parameters such as char, fromIndex, and string(Below are the syntaxes of all the polymorphic variants).

Syntax

Following is the syntax of the Java String indexOf() method −

public int indexOf(int ch) // first syntax
public int indexOf(int ch, int fromIndex)// second syntax
public int indexOf(String str)// third syntax
public int indexOf(String str, int fromIndex) // fourth syntax 

Parameters

  • ch − This is a character (Unicode code point).

  • fromIndex − This is starting index position.

  • str − This is the string.

Return Value

This method returns the index within this string of the first occurrence of the specified character.

Example

If the given character value does not occur in the current string, the indexOf() method returns -1.

In the following program, we are, creating a string literal with the value “TutorialsPoint”. Then, using the indexOf() method, we are trying to retrieve the index value of the specified character.

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      //create a string literal
      String str = "TutorialsPoint";
      System.out.println("The given string is: " + str);
      
      //initialize the char value
      char ch = 'x';
      System.out.println("The given char value is: " + ch);
      
      //using the indexOf() method
      System.out.println("The index of the '" + ch + "' is: " + str.indexOf(ch));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: TutorialsPoint
The given char value is: x
The index of the 'x' is: -1

Example

If we pass char and fromIndex value as an argument to this method, it returns the index of the character.

In the following example, we are instantiating the string class with the value “Hello world”. Using the indexOf() method, we are trying to retrieve the character ‘W’ at the specified fromIndex 1 in the given string.

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      // instantiate the string class
      String str = new String("Hello World");
      System.out.println("The given string is: " + str);
      
      //initialize the char and fromIndex values
      char ch = 'W';
      int fromIndex = 1;
      System.out.println("The given char and fromIndex values are: " + ch + " and " + fromIndex);
      
      // using the indexOf() method
      System.out.print("The index of the '" + ch + "' is: " + str.indexOf(ch, fromIndex));
   }
}

Output

Following is the output of the above program −

The given string is: Hello World
The given char and fromIndex values are: W and 1
The index of the 'W' is: 6

Example

If we pass the string as an argument to the method, the indexOf() method returns the index position of the specified string.

In this program, we are creating an object of the string class with the value “Java Programming”. Then, using the indexOf() method, we are trying to retrieve the index of the specified string value “Programming” in the current string.

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str = new String("Java Programming");
      System.out.println("The given string is: " + str);
      
      //initialize the string value
      String str1 = "Programming";
      System.out.println("The initialize substring is: " + str1);
      
      // using indexOf() method
      int index = str.indexOf(str1);
      System.out.println("The index of the '" + str1 + "' is: " + index);
   }
}

Output

The above program, produces the following output −

The given string is: Java Programming
The initialize substring is: Programming
The index of the 'Programming' is: 5

Example

If we pass string and fromIndex as an argument to this method, it returns the index position of the specified string.

In this example, we are creating a string literal with the value “Hello World”. Using the indexOf() method, we are trying to get the index of the string value “World” at the specified fromIndex 2.

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      // create string literal
      String str = "Hello World";
      System.out.println("The given string is: " + str);
      
      //initialize the string and fromIndex values
      String str1 = "World";
      int fromIndex = 2;
      System.out.println("The given string and from index values are: " + str1 + " and " + fromIndex);
      
      // using indexOf() method
      int index = str.indexOf(str1, fromIndex);
      System.out.println("The index of the '" + str1 + "' is: " + index);
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: Hello World
The given string and from index values are: World and 2
The index of the 'World' is: 6
java_lang_string.htm
Advertisements