在Java中,charAt()
是一個 String
類的方法,它返回給定索引處的 char
值
public class Main {
public static void main(String[] args) {
String example = "Hello, World!";
// 獲取索引為4的字符('o')
char ch = example.charAt(4);
System.out.println("Character at index 4: " + ch);
}
}
在這個例子中,我們創建了一個名為 example
的字符串變量,并使用 charAt()
方法獲取索引為4的字符。然后,我們將結果打印到控制臺。請注意,字符串索引從0開始,因此索引4對應于第五個字符。