indexOf() String Example


Many methods exist in java.lang.String class used for different string manipulations like checking the availability of a character, to extract a part of a string or to convert a data type to string etc. One such simple method is indexOf() String.

indexOf() method checks the existence of a single character or a group of characters (string) in the string.

indexOf() String is overloaded 4 times. Following are the method signatures.
  1. public int indexOf(int ch): Returns the index number of the specified character ch (or character in ASCII number, see notes after example) passed as parameter. The availability of the character ch is checked from left to right starting from index number 0 in the string. The string may contain multiple ch characters. First occurrence of the ch in the string is returned. After returning the index number of the first occurrence, further checking of ch occurrence is stopped. If the character ch is not found in the string, -1 is returned.
  2. public int indexOf(int ch, int fromIndex): In brief, the difference from the previous method is the checking position; earlier it was 0 position and now it is from fromIndex. Read further. Returns the index number of the specified character ch passed as parameter. The availability of the character ch is checked from left to right from fromIndex number in the string (in the previous method, availability is checked from 0 index position). The string may contain multiple ch characters. First occurrence of ch in the string from fromIndex position is returned. After returning the index number of the first occurrence, further checking of ch occurrence is stopped. If the character ch is not found in the string, -1 is returned.
  3. public int indexOf(String str): Previous method takes a single character as parameter. But this overloaded method takes string as parameter. The first occurrence of the str is checked in the actual string from 0 index number and if found, returns its index number. The availability is checked from left to right and if not found, returns -1.
  4. public int indexOf(String str, int fromIndex): Returns the index number of the specified string str passed as parameter. The availability of the string str is checked from left to right, but from fromIndex number in the string (in the previous method, availability is checked from 0 index position). The actual string may contain multiple str strings. First occurrence of str in the actual string from fromIndex position is returned. After returning the index number of the first occurrence, further checking of str occurrence is stopped. If the string str is not found, -1 is returned.

Note: Like equals() method, indexOf() checking is case-sensitive.

Following example on indexOf() String illustrates the usage of 4 overloaded methods.

public class StringMethodDemo
{
  public static void main(String args[])
  {
   String str = "ABCABCABC";  		     // index numbering starts form zero
			                     // 1st overloaded method
   int x = str.indexOf('B');
   System.out.println("B index position in str from 0 position: " + x); 
					// 2nd overloaded method
   System.out.println("B index position in str from index position 2: " + str.indexOf('B', 2));
					// 3rd overloaded method
   System.out.println("\nCAB index position in str from 0 position: " + str.indexOf("CAB")); 
					// 4th overloaded method
   System.out.println("CAB index position in str from 3 position: " + str.indexOf("CAB", 3)); 

   System.out.println("\nK index position in str from index position 0: " + str.indexOf('K'));
  }
}


indexOf() String
Output screen on indexOf() String

int x = str.indexOf('B');

In the above statement, B can be replaced by its ASCII value 66 as follows.

int x = str.indexOf(66);

Observe keenly the signature of the first overloaded method; the argument is int value and not char. Actually, when we pass char, char is converted to ASCII number and then evaluated.

Let us find the index numbers of all the characters in the given string str in a for loop.

for(int i = 0; i < str.length(); i++)
{
  System.out.println(str.charAt(i) + " index number is " + str.indexOf(str.charAt(i), i));
}

indexOf() String

A similar example exists: lastIndexOf() method usage.

charAt(int) method is used to return the character at the specified index position.

Leave a Comment

Your email address will not be published.