Java getChars() 方法
getChars() 方法將字元從字串複製到目標字元數組。
語法
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
參數
srcBegin -- 字串中要複製的第一個字元的索引。
srcEnd -- 字串中要複製的最後一個字元之後的索引。
dst -- 目標數組。
dstBegin -- 目標數組中的起始偏移量。
返回值
沒有返回值,但會拋出 IndexOutOfBoundsException 異常。
實例
public class Test { public static void main(String args[]) { String Str1 = new String("www.xuhuhu.com"); char[] Str2 = new char[6]; try { Str1.getChars(4, 10, Str2, 0); System.out.print("拷貝的字串為:" ); System.out.println(Str2 ); } catch( Exception ex) { System.out.println("觸發異常..."); } } }
以上程式執行結果為:
拷貝的字串為:zaixian