charAt方法:
java.lang.String.charAt()方法 返回 指定索引 处的 char值。索引范围 是从0 到length() - 1。
对于数组的索引,序列的第一个 char值 是在索引 为0,索引1,以此类推。。
这是String类中的关于这个方法的源代码:
public char charAt(int index) { if ((index < 0) || (index >= value.length)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; }
参数index 这是该指数的char值。。
这个方法返回这个字符串的指定索引处的char值。第一个char值得索引为0.。
如果index参数为负或不小于该字符串的长度会报异常IndexOutOfBoundsException ,这是个越界异常
public class CharAt { public static void main(String[] args) { String s = "bejing welcome you"; System.out.println(s.charAt(1)); System.out.println(s.charAt(5)); System.out.println(s.charAt(15)); } }
运行结果:
e
g
y
免责声明:文章源自网络,版权归原作者所有,如有侵犯联系删除。