Methods-String.charAt()

引用

http://www.yiibai.com/javalang/string_charat.html

描述
**java.lang.String.charAt() **方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推,
声明
以下是声明java.lang.String.charAt()方法
public char charAt(int index)

参数
index -- 这是该指数的char值.

返回值
此方法返回这个字符串的指定索引处的char值。第一个char值的索引为0.
异常
IndexOutOfBoundsException -- 如果index参数为负或不小于该字符串的长度.

实例
下面的示例演示使用的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)); } }
让我们编译和运行上面的程序,这将产生以下结果。它也会打印空白字符。
T p

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容