Java String.charAt ()

Java String.charAt ()

La méthodecharAt() renvoie le caractère à l'index spécifié. La valeur d'index doit être comprise entre 0 etString.length() – 1.

Signatures disponibles

public char charAt(int index)

Exemple

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

Jette

  • IndexOutOfBoundsException - si un index inexistant ou négatif est passé à la méthode

@Test(expected = IndexOutOfBoundsException.class)
public void whenCharAtOnNonExistingIndex_thenIndexOutOfBoundsExceptionThrown() {
    int character = "Paul".charAt(4);
}