java charat函数用法_Java String charAt()用法及代码示例
Java String charAt()方法返回指定索引处的字符。索引值应介于0到length()-1之间。签名:
public char charAt(int index)
参数:
index- Index of the character to be returned.
返回:
returns character at the specified position.
异常:
StringIndexOutOfBoundsException- If index is
negative or greater then the length of the
String.
例:展示charAt()方法的用法原理
// Java program to demonstrate
// working of charAt() method
class Gfg {
public static void main(String args[])
{
String s = "Welcome! to Geeksforgeeks Planet";
char ch = s.charAt(3);
System.out.println(ch);
ch = s.charAt(0);
System.out.println(ch);
}
}
输出:
c
W
// Java program to demonstrate
// working of charAt() method
class Gfg {
public static void main(String args[])
{
String s = "abc";
char ch = s.charAt(4);
System.out.println(ch);
}
}
输出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range:4
at java.lang.String.charAt(String.java:658)
at Gfg.main(File.java:9)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
