1. 程式人生 > >java.lang.String.charAt() 方法

java.lang.String.charAt() 方法

java.lang.String.charAt() 方法返回指定索引處的char值。索引範圍是從0length() - 1。對於陣列索引,序列的第一個char值是在索引為0,索引1,依此類推,

宣告

以下是宣告java.lang.String.charAt()方法

public charcharAt(int index)     

引數

·        index -- 這是該指數的char.

返回值

方法返回這個字串的指定索引處的char值。第一個char值的索引為0.

異常

·        IndexOutOfBoundsException -- 如果index引數為負或不小於該字串的長度

.

public class CharAt {
	
	 public static void main(String[] args) {
		  
		    String s = "bejing welcome you";
		                   
		    		    System.out.println(s.charAt(1));
		          
		    		    System.out.println(s.charAt(5));
		          
				    System.out.println(s.charAt(15));
		  }

}
執行結果:

      e

      g

      y