Java String.indexOf()

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

[source,java,gutter:,false]

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

Example

[source,java,gutter:,true]

@Test
public void whenCallIndexOf_thenCorrect() {
    String str = "foo";

    assertEquals(1, str.indexOf("o"));
    assertEquals(-1, str.indexOf("s"));
}