欢迎光临
我们一直在努力

字符串charat的方法,string. charAt

String的charAt方法(转)

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 便宜美国vps StringIndexOutOfBoundsException(index);
}
return value[index];
}
1
2
3
4
5
6
参数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));
}
}
1
2
3
4
5
运行结果:
e
g
y

87128088

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。