StringBuilder类的getChars()方法 (StringBuilder Class getChars() method)
getChars() method is available in java.lang package.
getChars()方法在java.lang包中可用。
getChars() method is used to copy all the characters from the given arguments (int src_st, int src_end) into another destination array of char type like char[] dest.
getChars()方法用于将给定参数(int src_st,int src_end)中的所有字符复制到另一个char类型的目标数组中,例如char [] dest。
In this method copy first character starts at index src_st and copying the last character ends at index src_end so all the copied characters will be placed in an array char[] dest and this array index starts at dest_st and ends at dest_beg+(src_end-src_beg)-1.
在此方法中,复制第一个字符开始于索引src_st ,复制最后一个字符结束于索引src_end,因此所有复制的字符都将放置在char [] dest数组中,并且此数组索引开始于dest_stg并结束于dest_beg +(src_end-src_beg) -1 。
getChars() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
getChars()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
This method may throw an exception at the time of copying and placing copied characters.
在复制和放置复制的字符时,此方法可能会引发异常。
- IndexOutOfBoundsException – This exception may throw when src_st < 0 or dest_st < 0 or src_st > src_end or src_end > length().
- IndexOutOfBoundsException-当src_st <0或dest_st <0或src_st> src_end或src_end> length()时,可能引发此异常。
- NullPointerException – This exception may throw when char[] array is null exists.NullPointerException-如果char []数组为null,则可能引发此异常。
Syntax:
句法:
public void getChars(int src_st, int src_end, char[] dest, int dest_st);
Parameter(s):
参数:
int src_st – represents the index to start copying.
int src_st –表示要开始复制的索引。
int src_end – represents the index to end copying.
int src_end –表示结束复制的索引。
int char[] dest – represents the array for copied elements.
int char [] dest –表示复制元素的数组。
int dest_beg – represents the index of starting position of char[] dest.
int dest_beg –表示char [] dest的起始位置的索引。
Return value:
返回值:
The return type of this method is void, it returns nothing.
此方法的返回类型为void ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example
// of getChars(int src_st, int src_end, char[] dest, int dest_st)
// method of StringBuilder
public class GetChars {
public static void main(String[] args) {
int src_st = 0;
int src_end = 4;
int dest_st = 0;
// Creating an StringBuilder object
StringBuilder st_b = new StringBuilder("Java World");
// Display st_b
System.out.println("st_b = " + st_b);
char[] dest = new char[] {
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j'
};
// By using getChars() method is to copy the chars from the
// given src_st to src_end of st_b and placed into dest[]
// and position of placing the copied chars starts at dest_st
st_b.getChars(src_st, src_end, dest, dest_st);
// Display destination array
for (char val: dest)
System.out.print("" + val);
}
}
Output
输出量
st_b = Java World
Javaefghij
翻译自: https://www.includehelp.com/java/stringbuilder-getchars-method-with-example.aspx