Java.lang.String.charAt() 方法
描述
java.lang.String.charAt() 方法返回指定索引处的 char 值。 索引的范围从 0 到 length() - 1。序列的第一个 char 值在索引 0 处,下一个在索引 1 处,依此类推,与数组索引一样。
声明
以下是 java.lang.String.charAt() 方法的声明。
public char charAt(int index)
参数
index − 这是 char 值的索引。
返回值
此方法返回此字符串指定索引处的 char 值。 第一个 char 值位于索引 0 处。
异常
IndexOutOfBoundsException − 如果 index 参数为负数或不小于此字符串的长度。
示例
下面的例子展示了 java.lang.String.charAt() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "This is tutorialspoint";
// prints character at 1st location
System.out.println(str.charAt(0));
// prints character at 5th location i.e white-space character
System.out.println(str.charAt(4));
// prints character at 18th location
System.out.println(str.charAt(17));
}
}
Let us compile and run the above program, this will produce the following result.It will also print white-space character.
T p

