引用
描述
**java.lang.String.charAt() **方法返回指定索引處的char值花吟。索引范圍是從0到length() - 1蠢熄。對(duì)于數(shù)組索引钝鸽,序列的第一個(gè)char值是在索引為0,索引1,依此類推,
聲明
以下是聲明java.lang.String.charAt()方法
public char charAt(int index)
參數(shù)
index -- 這是該指數(shù)的char值.
返回值
此方法返回這個(gè)字符串的指定索引處的char值。第一個(gè)char值的索引為0.
異常
IndexOutOfBoundsException -- 如果index參數(shù)為負(fù)或不小于該字符串的長(zhǎng)度.
實(shí)例
下面的示例演示使用的java.lang.String.charAt()方法.
package com.yiibai; import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "This is yiibai"; // prints character at 1st location System.out.println(str.charAt(0)); // prints character at 5th location i.e white-space character System.out.println(str.charAt(4)); // prints character at 18th location System.out.println(str.charAt(17)); } }
讓我們編譯和運(yùn)行上面的程序翰撑,這將產(chǎn)生以下結(jié)果。它也會(huì)打印空白字符啊央。
T p