In Java, String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.
Variants of indexOf() Method
There are four variants of the indexOf() method are mentioned below:
- int indexOf()
- int indexOf(char ch, int strt)
- int indexOf(String str)
- int indexOf(String str, int strt)
1. int indexOf()
This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.
Syntax:
int indexOf(char ch )
Parameters:
ch : a character.
Below is the implementation of the above method
Java
public class Index1 {
public static void main(String args[])
{
String gfg = new String( "Welcome to geeksforgeeks" );
System.out.print( "Found g first at position : " );
System.out.println(gfg.indexOf( 'g' ));
}
}
|
Output
Found g first at position : 11
2. int indexOf(char ch, int strt)
This method returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1, if the character does not occur.
Syntax:
int indexOf(char ch, int strt)
Parameters:
ch :a character.
strt : the index to start the search from.
Example of the above method:
Java
public class Index2 {
public static void main(String args[])
{
String gfg = new String( "Welcome to geeksforgeeks" );
System.out.print(
"Found g after 13th index at position : " );
System.out.println(gfg.indexOf( 'g' , 13 ));
}
}
|
Output
Found g after 13th index at position : 19
3. int indexOf(String str)
This method returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.
Syntax:
int indexOf(String str)
Parameters:
str : a string.
Example of the above method:
Java
public class Index3 {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
String subst = new String( "geeks" );
System.out.print(
"Found geeks starting at position : " );
System.out.print(Str.indexOf(subst));
}
}
|
Output
Found geeks starting at position : 11
4. int indexOf(String str, int strt)
This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned.
Syntax:
int indexOf(String str, int strt)
Parameters:
strt: the index to start the search from.
str : a string.
Java
public class Index4 {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
String subst = new String( "geeks" );
System.out.print(
"Found geeks(after 14th index) starting at position : " );
System.out.print(Str.indexOf(subst, 14 ));
}
}
|
Output
Found geeks(after 14th index) starting at position : 19
Some Related Applications
Finding out if a given character (maybe anything in upper or lower case) is a vowel or consonant.
Implementation is given below:
Java
class Vowels {
public static boolean vowel( char c)
{
return "aeiouAEIOU" .indexOf(c) >= 0 ;
}
public static void main(String[] args)
{
boolean isVowel = vowel( 'a' );
if (isVowel)
System.out.println( "Vowel" );
else
System.out.println( "Consonant" );
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Jun, 2023
Like Article
Save Article