The method indexOf() returns the first occurrence index of a character or a String in another String. We can pass the index of the character to start searching from.

Note that the method returns -1 if the passed value is not found.

Available Signatures

public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)


public void whenCallIndexOf_thenCorrect() {
    String str = "foo";
    assertEquals(1, str.indexOf("o"));
    assertEquals(-1, str.indexOf("s"));