âå符串â 转 âå符æ°ç»âï¼æ以ä¸æ¹æ³
1.éè¿å符串çtoCharArray()æ¹æ³
/*String.toCharArray æ¹æ³ ,ä½ç¨ï¼å°å符串转æ¢ä¸ºå符æ°ç»ã*/
String s=new String("abc");
char[] ch=s.toCharArray();
2.éè¿å¾ªç¯åå符串çcharAt()æ¹æ³
/*public char charAt(intindex)
*è¿åå¤äºindexä½ç½®ä¸çå符ï¼indexçèå´æ¯[0,s.length()-1]ï¼å符串ä¸ç第ä¸ä¸ªå符ä½äºindex 0ä¸ï¼ä¸ä¸ä¸ªå符ä½äºindex 1ä¸ï¼å¹¶ä»¥æ¤ç±»æ¨ï¼ç±»ä¼¼äºæ°ç»ä¸æ ç´¢å¼ã
*æåºå¼å¸¸ï¼IndexOutOfBoundsExceptionï¼å½åæ°ä¸ºè´æ°æè
åæ°ä¸å°äºå符串çé¿åº¦æ¶æåºã
*/
String s=new String("abc");
char[] ch = new char[s.length()];
for(int i=0;i<s.length();i++){
ch[i]=s.charAt(i);
}
3.éè¿å符串çgetCharsæ¹æ³
/*
*public void getChars(int start,int end,char c[],int offset)
该æ¹æ³çä½ç¨æ¯å°å½åå符串ä»startå°end-1ä½ç½®ä¸çå符å¤å¶å°å符æ°ç»cä¸ï¼å¹¶ä»cçoffsetå¤å¼å§åæ¾
*/
String s=new String("abc");
char[] ch = new char[s.length()];
s.getChars(0, s.length(), ch, 0);