1. 程式人生 > >java中charAt()方法的使用

java中charAt()方法的使用

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