Course – LS – All

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

>> CHECK OUT THE COURSE

The method charAt() returns the character at the specified index. The index value must be between 0 and String.length() – 1.

Available Signatures

public char charAt(int index)

Example

@Test
public void whenCallCharAt_thenCorrect() {
    assertEquals('P', "Paul".charAt(0));
}

Throws

  • IndexOutOfBoundsException – if a non-existent or a negative index is passed to the method
@Test(expected = IndexOutOfBoundsException.class)
public void whenCharAtOnNonExistingIndex_thenIndexOutOfBoundsExceptionThrown() {
    int character = "Paul".charAt(4);
}
Course – LS – All

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

>> CHECK OUT THE 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.