【Java】Java中charAt()方法的使用

说明

java.lang.String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推

声明

public char charAt(int index)

参数

index -- 这是该指数的char值

返回值

此方法返回这个字符串的指定索引处的char值。第一个char值的索引为0.

异常

IndexOutOfBoundsException -- 如果index参数为负或不小于该字符串的长度.

示例

public class Test {
    public static void main(String[] args) {
        String s ="abc";
        System.out.println(s.charAt(2));
    }
}
运行结果是:
c
原文地址:https://www.cnblogs.com/jxd283465/p/11939916.html