I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

This article is part of a series:
• Java String.String()
• Java String.indexOf()
• Java String.codePointCount()
• Java String.codePointAt()
• Java String.concat()
• Java String.contains()
• Java String.copyValueOf()
• Java String.endsWith()
• Java String.format()
• Java String.getBytes()
• Java String.indexOf()
• Java String.intern()
• Java String.isEmpty()
• Java String.lastIndexOf()
• Java String.regionMatches()
• Java String.replace()
• Java String.replaceAll()
• Java String.split()
• Java String.startsWith()
• Java String.subSequence()
• Java String.substring()
• Java String.toLowerCase()
• Java String.toUpperCase()
• Java String.trim()
• Java String.valueOf()

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)

Example

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

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS