原文: https://beginnersbook.com/2013/12/java-string-getchars-method-example/
方法getChars()用于将String字符复制到字符数组。
public void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin)
参数说明:
srcBegin - 要复制的字符串中第一个字符的索引。
srcEnd - 要复制的字符串中最后一个字符后的索引。
dest - 目标字符数组,其中String中的字符被复制。
destBegin - 数组中的索引,从字符将被推入数组开始。
抛出IndexOutOfBoundsException - 如果出现以下任何一种情况:
(srcBegin < 0)srcBegin小于零。
(srcBegin > srcEnd)srcBegin大于srcEnd。
(srcEnd > string.length)srcEnd大于此字符串的长度。
(destBegin < 0)destBegin为负。
dstBegin+(srcEnd-srcBegin)大于dest.length。
示例:getChars()方法
public class GetCharsExample{public static void main(String args[]){String str = new String("This is a String Handling Tutorial");char[] array = new char[6];str.getChars(10, 16, array, 0);System.out.println("Array Content:" );for(char temp: array){System.out.print(temp);}char[] array2 = new char[]{'a','a','a','a','a','a','a','a'};str.getChars(10, 16, array2, 2);System.out.println("Second Array Content:" );for(char temp: array2){System.out.print(temp);}}}
输出:
Array Content:StringSecond Array Content:aaString
