String indexof() java

A string is an array of characters and each character stores in the specific index. The string indexOf() method is used to get the index of a particular character in the string. You can get the index of any character or substring by use of string indexof() java. It returns the number -1, if the given character or substring is doesn’t exist in the current string.

The indexOf() is an overloaded method in the String class. It has 4 types :

  1. indexOf(int characterName);
  2. indexOf(int characterName, int fromIndex);
  3. indexOf(String subStringName);
  4. indexOf(String subStringName, int fromIndex);
String indexof() java

1. indexOf(int characterName)

This method accepts only one parameter. It returns the index value of the first occurrence of the specified character. Important point: As you can see this method can accept the int parameter, but we can give it character also because it expects to encode the codepoint.

stringName.indexOf(int characterName)

stringName: The name of the string in which you want to find the index of a given character. characterName: Give the character whom position you want to find in the current string.

class ExampleOfIndexOf
 {  
	 public static void main(String args[])
	 {  
		String name = "JAVA WINGS";  
		int index = name.indexOf('V');
		System.out.println("The index of given character is = "+index);
	}
}  

Output: The index of given character is = 2

2. indexOf(int characterName, int fromIndex)

This method accepts two parameters. One parameter is the name of a character and another the index to start the search from. It returns the index value of the first occurrence of the specified character.

stringName.indexOf(int characterName, int fromIndex)

stringName: The name of the string in which you want to find the index of a given character.
characterName: Give the character whom position you want to find in the current string.
fromIndex: the index to start the search from

class ExampleOfIndexOf
{  
    public static void main(String args[])
    {  
 	String name = "JAVA WINGS";  
	int index = name.indexOf('A');
	System.out.println("The index of first occurrence of given character is = "+index);
			 
	int index1 = name.indexOf('A', 2);
	System.out.println("The index of character after the specified index for search = 
        "+index1);	
   }
}

Output: The index of first occurrence of given character is = 1
The index of character after the specified index for search = 3

3. indexOf(String subStringName)

This method accepts only one parameter. It returns the index value of the first occurrence of the specified substring.

stringName.indexOf(String subStringName)

stringName: The name of string in which you want to find the index of the given substring.
subStringName: Give the subString whom position you want to find in the current string.

class ExampleOfIndexOf
{  
   public static void main(String args[])
   {  
	String name = "JAVA WINGS";  
	int index = name.indexOf("WING");
	System.out.println("The index of first occurnce of given subString is = "+index);	 
   }
}  

Output: The index of first occurrence of given subString is = 5

4. indexOf(String subStringName, int fromIndex)

This method accepts two parameters. One parameter is the name of substring and another the index to start the search from. It returns the index value of the first occurrence of the specified substring.

stringName.indexOf(String subString, int fromIndex)

stringName: The name of string in which you want to find the index of the given substring.
subStringName: Givethe substring whom position you want to find in the current string.
fromIndex: the index to start the search from.

class ExampleOfIndexOf
{  
  public static void main(String args[])
  {  
	String name = "JAVA WINGS";  			 
	int index1 = name.indexOf("WINGS", 3);
        System.out.println("The index of character after the specified index for search = 
        "+index1);	
   }
}  

Output: The index of character after the specified index for search = 5

4 thoughts on “String indexof() java”

Leave a Comment