charAt()方法
char数据类型是16位无符号Java基元数据类型。它表示Unicode字符。
请注意,char是无符号数据类型。因此,char变量不能为负值。
字符数据类型的范围为0到65535,这与Unicode集的范围相同。
charAt() 方法用于返回指定索引处的字符。
索引范围为从 0 到 length() - 1。
语法
public char charAt(int index)
参数
index – 字符的索引。
返回值
返回指定索引处的字符。
实例
public class Test {
public static void main(String args[]) {
String s = "www.runoob.com";
char result = s.charAt(6);
System.out.println(result);
}
}
以上程序执行结果为:
n
toCharArray() 方法
toCharArray() 方法将字符串转换为字符数组。
语法
public char[] toCharArray()
参数
无
返回值
字符数组。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println( Str.toCharArray() );
}
}
以上程序执行结果为:
返回值 :www.runoob.com