Java StringBuilder getChars() 方法



**Java StringBuilder getChars()** 方法將此序列中的字元複製到目標字元陣列 **dst** 中。在 Java 中,陣列是一個包含相同資料型別元素的物件。

第一個要複製的字元位於索引 **srcBegin** 處;最後一個要複製的字元位於索引 **srcEnd - 1** 處。要複製的字元總數為 **srcEnd - srcBegin**。這些字元被複制到從索引 dstBegin 開始到索引 **dstbegin + (srcEnd-srcBegin) – 1** 結束的 dst 的子陣列中。

語法

以下是 **Java StringBuilder getChars()** 方法的語法:-

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

引數

  • **srcBegin** - 表示從該偏移量開始複製。
  • **srcEnd** - 表示在該偏移量停止複製。
  • **dst** - 這是要將資料複製到的陣列。
  • **dstBegin** - 這是 dst 中的偏移量。

返回值

此方法不返回值。

示例:獲取子字串的字元陣列

如果目標字元陣列 **不為空**,並且索引值是正數,則 **getChars()** 方法會將此序列的字元複製到字元陣列中。

在下面的程式中,我們使用值 **“Welcome to TutorialsPoint”** 建立了一個 **StringBuilder 類** 的物件。使用 **getChars()** 方法,我們嘗試將字元的值複製到目標陣列中,**srcBegin 索引為 3**,**srcEnd 索引為 5**,**dstBegin 索引為 0**。

public class Demo {
   public static void main(String[] args) {
      // creating an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("Welcome to TutorialsPoint");
      System.out.println("The given string value is: " + sb);
      //create an array of character
      char dst[] = {'A','B','C'};
      System.out.print("The characrer array elements are: ");
      for(int i = 0; i<dst.length; i++) {
         System.out.print(dst[i] + " ");
      }
      //initialize the srcBegin, srcEnd, dstBegin
      int srcBegin = 3;
      int srcEnd = 5;
      int dstBegin = 0;
      System.out.println("\nThe values of srcBegin, srcEnd, and dstBegin are: " + srcBegin + " , " + srcEnd + " and " + dstBegin);
      //using the getChars() method
      sb.getChars(srcBegin, srcEnd, dst, dstBegin);
      System.out.print("The new copied character array elements are: ");
      for(int i = 0; i<dst.length; i++) {
         System.out.print(dst[i] + " ");
      }
   }
}

輸出

執行上述程式後,將產生以下結果:-

The given string value is: Welcome to TutorialsPoint
The characrer array elements are: A B C
The values of srcBegin, srcEnd, and dstBegin are: 3 , 5 and 0
The new copied character array elements are: c o C

示例:獲取空子字串的字元陣列時遇到 NullPointerException

如果目標字元陣列為 **null**,則 getChars() 方法會丟擲 **NullPointerException**。

在下面的程式中,我們使用值 **“Java Programming”** 例項化了 **StringBuilder 類**。然後,我們建立了一個值為 null 的字元陣列。使用 **getChars()** 方法,我們嘗試將此序列的字元複製到包含 null 值的字元陣列中。

public class Demo {
   public static void main(String[] args) {
      try {
         // instantiating the StringBuilder
         StringBuilder sb = new StringBuilder("Java Programming");
         System.out.println("The given string value is: " + sb);
         //create an array of character
         char dst[] = null;
         System.out.println("The characrer array elements are: " + dst);
         //initialize the srcBegin, srcEnd, dstBegin
         int srcBegin = 0;
         int srcEnd = 5;
         int dstBegin = 0;
         System.out.println("The values of srcBegin, srcEnd, and dstBegin are: " + srcBegin + " , " + srcEnd + " and " + dstBegin);
         //using the getChars() method
         sb.getChars(srcBegin, srcEnd, dst, dstBegin);
         System.out.println("The new copied character array elements are: " + dst);
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:-

The given string value is: Java Programming
The characrer array elements are: null
The values of srcBegin, srcEnd, and dstBegin are: 0 , 5 and 0
java.lang.NullPointerException: Cannot read the array length because "dst" is null
        at java.base/java.lang.AbstractStringBuilder.getChars(AbstractStringBuilder.java:499)
        at java.base/java.lang.StringBuilder.getChars(StringBuilder.java:289)
        at Demo.main(Demo.java:21)
Exception: java.lang.NullPointerException: Cannot read the array length because "dst" is null

示例:獲取無效索引的字元陣列時遇到 IndexOutOfBoundsException

如果 srcBegin 和 dstBegin 的給定值為 **負數**,則此方法會丟擲 **IndexOutOfBoundsException**。

在下面的示例中,我們使用值 **“HelloWorld”** 建立了一個 **StringBuilder 類** 的物件。使用 **getChars()** 方法,我們嘗試將此序列的字元複製到字元陣列中,srcBegin 索引和 dstBegin 索引為 **-1**。

public class Demo {
   public static void main(String[] args) {
      try {
         // instantiating the StringBuilder
         StringBuilder sb = new StringBuilder("HelloWorld");
         System.out.println("The given string value is: " + sb);
         //create an array of character
         char dst[] = {'a','b','c'};
         System.out.println("The characrer array elements are: " + dst);
         //initialize the srcBegin, srcEnd, dstBegin
         int srcBegin = -1;
         int srcEnd = 5;
         int dstBegin = -1;
         System.out.println("The values of srcBegin, srcEnd, and dstBegin are: " + srcBegin + " , " + srcEnd + " and " + dstBegin);
         //using the getChars() method
         sb.getChars(srcBegin, srcEnd, dst, dstBegin);
         System.out.println("The new copied character array elements are: " + dst);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下結果:-

The given string value is: HelloWorld
The characrer array elements are: [C@76ed5528
The values of srcBegin, srcEnd, and dstBegin are: -1 , 5 and -1
java.lang.StringIndexOutOfBoundsException: Range [-1, 5) out of bounds for length 10
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.getChars(AbstractStringBuilder.java:497)
   at java.base/java.lang.StringBuilder.getChars(StringBuilder.java:289)
   at Demo.main(Demo.java:21)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 5) out of bounds for length 10
java_lang_stringbuilder.htm
廣告