java中charAt()方法的使用

来源:互联网 发布:网络彩票2016最新消息 编辑:程序博客网 时间:2024/06/06 20:30

charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法.
charAt()方法返回指定索引位置的char值。索引范围为0~length()-1.
如: str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.

package com.xumingjie.s;


public class Cartat {
 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(6));
 }

}

结果如下:

T
 
s

1 0
原创粉丝点击