public char[] toCharArray()
該方法的作用是返回一個字符數組,該字符數組中存放了當前字符串中的所有字符
eg:
1 public class class6_3 2 3 { 4 5 public static void main(String args[]) 6 7 { 8 9 String s1=new String("我是中國人"); 10 11 char[] c=s1.toCharArray(); 12 13 System.out.println("數組c的長度為:"+c.length); 14 15 System.out.println(c); 16 17 System.out.println(new String(c)); 18 19 String s2=new String(c,1,2); 20 21 System.out.println(s2); 22 } 23 24 }
運行結果:
數組c的長度為:5
我是中國人
我是中國人
是中