Cos'è il metodo Java String charAt()?
Il metodo charAt() della classe string Java restituisce il carattere nell'indice specificato di una stringa. Questo indice specificato viene fornito come parametro e varia da 0 a n-1, dove n rappresenta la lunghezza della stringa. Sintassipublic char charAt(int index)
Parametri Il metodo charAt() riceve un intero positivo perché la lunghezza non è mai inferiore a 0. Restituisce Restituisce sempre un valore char dell'indice specificato ma se il valore del parametro è negativo o superiore alla lunghezza della stringa, genera un'eccezione.
Esempi di metodi Java String charAt()
class Main {
public static void main(String[] args) {
String str = "Java charAt() method example";
// picking the A character of the string str at index 9 using charAt() method
System.out.println(str.charAt(9));
// picking the ( character of the string str at index 11 using charAt() method
System.out.println(str.charAt(11));
// picking the ) character of the string str at index 12 using charAt() method
System.out.println(str.charAt(12));
}
}
Produzione
ÁA ( )
GO TO FULL VERSION