BlogsDope image BlogsDope

Java String indexOf() Method

Feb. 24, 2021 JAVA FUNCTION STRING 1040

Java String class provides an inbuilt method to get the index of a specified character or a substring in Java String. There are 4 variations of this method in the String class are-

Note - ​It also counts spaces as index positions.

indexOf(char ch)

This method is used to get the first occurrence of a character in a given string.

Example

  • Input: "code".indexOf('o')

       Output:1    //second character of string "code" is 'o'.

  • Input:"hello".indexOf('h')

       Output:0   //string "hello" starts with character 'h'.

Syntax

String.indexOf(char ch);

Parameter

ch: char value i.e. a single character e.g. 'c'.

Return

This method returns an integer value i.e the first occurrence of the given character in the string.

Program

package codesdope;
import java.util.*;
public class codesdope 
{
	public static void main(String[] args)
	{
        String s="welcome to codesdope";
        System.out.println(s.indexOf('w')); //case-1
        System.out.println(s.indexOf('t')); //case-2
        System.out.println(s.indexOf('z')); //case-3
	}
}

0

8

-1

  • In the first case, we are finding the first occurrence of the character 'w' using the indexOf() method and its first occurrence is at the zeroth (0) position of the string as the string starts from character 'w'.
  • In the second case, we are finding the first occurrence of the character 't' using the indexOf() method and its first occurrence is at the eight (8) position when we start to count from starting including spaces till character t.
  • In the last case, we are finding the first occurrence of the character 'z' using the indexOf() method and character 'z' is not present in the entire string. Therefore, it returned -1.

indexOf(char ch, int fromIndex)

This method is used to find the first occurrence of character ch in the given string after the specified index “fromIndex”. For example, if the indexOf() method is called like this str.indexOf(‘C’, 2) then it would start looking for the character ‘C in string str after the index 2.

Example

  • Input:"Are you hungry?".indexOf('u',8)

       Output:9    // character 'u' is at 9th position after 8th position in a given string.

  • Input: "Hello coder".indexOf('o',6)

       Output:7   //character 'o' is at the 7th position after 6th position in a given string.

Syntax

String.indexOf(char ch,int fromIndex);

Parameter

There are two parameters used in this method are-
ch :char value i.e. a single character e.g. 'c'.
fromIndex: It is the index position from where we need to start searching.

Return

This method returns an integer value i.e the first occurrence of the given character in the string after the specified index "fromIndex".

Program

package codesdope;
import java.util.*;
public class codesdope 
{
	public static void main(String[] args)
	{
		String s="coding coding";
		System.out.println("The First occurrence of c:"+s.indexOf('c')); //case-1
		System.out.println("Next occurrence of c:"+s.indexOf('c',1));   //case-2   
	}
}

The first occurrence of c:0 

Next occurrence of c:7

For finding the first occurrence of a character in the string we use indexOf(char ch) as in case-1. But if we need to find out the next occurrence of a character after a specified index like in case-2, then we need to use indexOf(char ch, int from Index).

indexOf(String str)

This method is used to get the first occurrence of a substring in a given string.

Example

  • Input: "Do you like coding".indexOf("you")

       Output:3     // string "you" starts with position 3 in the given string s.

  • Input: "Do you like coding".indexOf("love")

       Output:-1    //"love" string is not present in the given string s.

Syntax

String.indexOf(string str);

Parameter

str: a string that is needed for the search.

Return

This method returns an index value i.e. the index of string str in a particular String.

Program

package codesdope;
import java.util.*;
public class codesdope 
{
	public static void main(String[] args)
	{
		String s="Welcome to Codesdope";
		System.out.println(s.indexOf("Codesdope"));  //case-1
		System.out.println(s.indexOf("to"));        //case-2
		System.out.println(s.indexOf("coding"));    //case-3
	}
}

​11

8

-1

  • In the first case, we are finding the first occurrence of the string"Codesdope"  using the indexOf() method and it returned 11 because "Codesdope" starts from 11th position in the given string s.
  • In the second case, we are finding the first occurrence of the string"of" using the indexOf() method and it returned 8 because "to" starts from 8th position in the given string s.
  • In the last case, we are finding the first occurrence of the string "coding" using the indexOf() method and it returned -1 because the string "coding" is not present in the given string s. 

indexOf(String str, int fromIndex)

This method is used to get the first occurrence of a substring in a given string after a specified index.For example, if the indexOf() method is called like this str.indexOf("abc", 2) then it would start looking for the string "abc" in string str after the index 2.

Example

  • Input: "I felt happy because I saw the others were happy".indexOf("happy",10)

       Output:43

  • Input: "God shall be All in All".indexOf("All",15)

       Output:20

Syntax

String.indexOf(string str,int fromIndex);

Parameter

There are two parameters used in this method are-
str: a string that is needed for the search.
fromIndex: It is the index position from where we need to start searching.

Return

This method returns an index value i.e. the index of substring str in a particular String after a specified index "fromIndex".

Program

package codesdope;
import java.util.*;
public class codesdope 
{
	public static void main(String[] args)
	{
		String s="I love java language as well as python language";
		System.out.println("The First occurrence of language word:"+s.indexOf("language"));
		System.out.println("Next occurance of language word:"+s.indexOf("language",13));
	}
}

​​​The first occurrence of language word:12 

Next occurrence of language word:39

For finding the first occurrence of a character in the string we use indexOf(char ch) as in case-1 but if we need to find out the next occurrence of a character after a specified index like in case-2 then we need to use indexOf(char ch, int from Index).


Liked the post?
Editor's Picks
0 COMMENT

Please login to view or add comment(s).