StringBuilder類的getChars(int srcBegin,int srcEnd,char [] dst,int dstBegin)方法將以給定index:srcBegin開頭的字符從StringBuilder包含的String複製到index:srcEnd-1到作為參數傳遞的char數組中發揮作用。
- 字符從StringBuilder複製到數組dst []中,開始於index:dstBegin,結束於index:dstbegin +(srcEnd-srcBegin)– 1。
- 從StringBuilder複製到數組的第一個字符位於索引srcBegin,而要複製的最後一個字符位於索引srcEnd-1。
- 要複製的字符總數等於srcEnd-srcBegin。
用法:
public void getChars(int srcBegin, int srcEnd,
char[] dst, int dstBegin)
參數:此方法接受四個不同的參數:
- srcBegin:代表我們必須開始複製的索引。
- srcEnd:表示我們必須停止複製的索引。
- dst:表示要將數據複製到的數組。
- dstBegin:代表目標數組的索引,我們開始粘貼複製的數據。
返回值:此方法不返回任何內容。
異常:在以下情況下,此方法將引發StringIndexOutOfBoundsException:
- srcBegin
- dstBegin
- srcBegin> srcEnd
- srcEnd> this.length()
- dstBegin + srcEnd-srcBegin> dst.length
下麵的程序演示StringBuilder類的getChars()方法:
示例1:
// Java program to demonstrate
// the getChars() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("WelcomeGeeks");
// print string
System.out.println("String = "
+ str.toString());
// create a char Array
char[] array = new char[7];
// get char from index 0 to 7
// and store in array start index 0
str.getChars(0, 7, array, 0);
// print char array after operation
System.out.print("Char array contains : ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
輸出:
String = WelcomeGeeks Char array contains : W e l c o m e
示例2:
// Java program to demonstrate
// the getChars() Method.
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("evil dead_01");
// print string
System.out.println("String = "
+ str.toString());
// create a char Array
char[] array = new char[10];
// initialize all character to _(underscore).
Arrays.fill(array, '_');
// get char from index 5 to 9
// and store in array start index 3
str.getChars(5, 9, array, 3);
// print char array after operation
System.out.print("char array contains : ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
輸出:
String = evil dead_01 char array contains : _ _ _ d e a d _ _ _
示例3:演示StringIndexOutOfBoundException
// Java program to demonstrate
// exception thrown by the getChars() Method.
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("evil dead_01");
// create a char Array
char[] array = new char[10];
// initialize all character to _(underscore).
Arrays.fill(array, '_');
try {
// if start is greater then end
str.getChars(5, 2, array, 0);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
輸出:
Exception: java.lang.StringIndexOutOfBoundsException: srcBegin > srcEnd
相關用法
- Java String getChars()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuilder setLength()用法及代碼示例
- Java StringBuilder setCharAt()用法及代碼示例
- Java StringBuilder codePointAt()用法及代碼示例
- Java StringBuilder capacity()用法及代碼示例
- Java StringBuilder deleteCharAt()用法及代碼示例
- Java StringBuilder codePointCount()用法及代碼示例
- Java StringBuilder ensureCapacity()用法及代碼示例
- Java StringBuilder subSequence()用法及代碼示例
- Java StringBuilder charAt()用法及代碼示例
- Java StringBuilder codePointBefore()用法及代碼示例
- Java StringBuilder Class用法及代碼示例
- Java StringBuilder reverse()用法及代碼示例
- Java StringBuilder delete()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder getChars() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。