Java StringBuilder getChars()方法与示例
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-sr??c_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 objectStringBuilder st_b = new StringBuilder("Java World");// Display st_bSystem.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_stst_b.getChars(src_st, src_end, dest, dest_st);// Display destination arrayfor (char val: dest)System.out.print("" + val);} }Output
輸出量
st_b = Java World Javaefghij翻譯自: https://www.includehelp.com/java/stringbuilder-getchars-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java StringBuilder getChars()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java timezone_Java T
- 下一篇: DNF谁最给力的