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"));
Next »
Java String.intern()
« Previous
Java String.getBytes()
Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.