首页 > 编程知识 正文

字符串charat的方法,string. charAt

时间:2023-05-04 13:13:27 阅读:285944 作者:1912

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 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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。