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)
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
Return
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)
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
Return
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)
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
Return
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)
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
Return
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)
.