方法 java.lang.String.charAt(int index) 返回 String 对象中指定 index
参数处的字符。
在Java 中,字符串内部存储在 char 数组中。
欢迎来到之路教程(on itroad-com)
ava String charAt() 方法示例
public class StringExample { public static void main(String[] args) throws Exception { String siteName = "onitroad.com"; char c1 = siteName.charAt(0); // 获取第1个字符 char c2 = siteName.charAt(siteName.length() - 1); // 获取最后一个字符 char c3 = siteName.charAt( 5 ); System.out.println(c1); System.out.println(c2); System.out.println(c3); char c4 = siteName.charAt( 30 ); // 无效索引,将导致StringIndexOutOfBoundsException异常 } }
charAt() 方法参数
唯一的方法参数是 index
。
它必须是 int
类型。
参数index 必须等于大于 0, 小于字符串字符的长度
,例如:str.length()-1
无效的索引参数将导致 StringIndexOutOfBoundsException
异常。
日期:2020-09-17 00:10:09 来源:oir作者:oir