Java String IndexOf Sample Code

The String.indexOf() method is used to find the index of the first occurrence of a character or string within another string. The String.indexOf() method goes well with the String.substring() method to extract pieces from a String.

There are 4 different versions of the indexOf() method. While all of the methods return an integer, watch of the methods has different input parameters. Each of the four Java examples is explained below.

Method Arguments Return Description
indexOf() character int Will return the index within this string of the first occurrence of the input character.
indexOf() character
int
int Will return the index within this string of the first occurrence of the input character, starting the search at the specified index.
indexOf() String int Will return the index within this string of the first occurrence of the input substring.
indexOf() String
int
int Will return the index within this string of the first occurrence of the input substring, starting at the specified index.


Java String IndexOf(character) Example

This code example uses the indexOf(character) to find the position of the first occurrence of the character ‘C’.

//String.indexOf(character) example
String jcdString1 = "JavaCodeDepot.com";
char jcdChar1 = 'C';
int jcdInt1 = jcdString1.indexOf(jcdChar1);
System.out.println("Found "+jcdChar1+" at position "+jcdInt1);
Found C starting at position 4


Java String IndexOf(character, int) Example

This code example uses the indexOf(character, int) to find the position of the character ‘e’ starting at position 8 in the String.

//String.indexOf(character, int) example
String jcdString2 = "JavaCodeDepot.com";
int jcdStartAt = 8;
char jcdChar2 = 'e';
int jcdInt2 = jcdString2.indexOf(jcdChar2, jcdStartAt);
System.out.println("Found "+jcdChar2+" at position "+jcdInt2);
Found e starting at position 9


Java String IndexOf(String) Example

This code example uses the indexOf(String) to find the position of the string ‘Code’ within the base String.

//String.indexOf(String) example
String jcdString3 = "JavaCodeDepot.com";
String jcdFind3 = "Code";
int jcdInt3 = jcdString3.indexOf(jcdFind3);
System.out.println("Found "+jcdFind3+" starting at position "+jcdInt3);
Found Code starting at position 4


Java String IndexOf(String, int) Example

This code example uses the indexOf(String, int) to find the position of ‘Java’ after skipping 5 positions forward in the String index.

//String.indexOf(String, int) example
String jcdString4 = "JavaCodeDepot.com Contains Many Java Samples";
String jcdFind4 = "Java";
int jcdStartAt4 = 5;
int jcdInt4 = jcdString4.indexOf(jcdFind4, jcdStartAt4);
System.out.println("Found "+jcdFind4+" starting at position "+jcdInt4);
Found Java starting at position 32


Each of the four String.indexOf() methods will return a -1 if the index of the specific character or String can be found.



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *