Java StringBuffer getChars() 方法



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

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

語法

以下是Java StringBuffer getChars() 方法的語法:

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

引數

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

返回值

此方法不返回值。

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

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

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

public class Demo {
   public static void main(String[] args) {
      // creating an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("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”例項化了StringBuffer 類。然後,我們建立了一個值為 null 的字元陣列。使用getChars() 方法,我們嘗試將此序列的字元複製到包含 null 值的字元陣列中。

public class Demo {
   public static void main(String[] args) {
      try {
         // instantiating the StringBuffer
         StringBuffer sb = new StringBuffer("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.StringBuffer.getChars(StringBuffer.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”建立了一個StringBuffer 類的物件。使用getChars() 方法,我們嘗試將此序列的字元複製到字元陣列中,在 srcBegin 索引和 dstBegin 索引-1處。

public class Demo {
   public static void main(String[] args) {
      try {
         // instantiating the StringBuffer
         StringBuffer sb = new StringBuffer("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.StringBuffer.getChars(StringBuffer.java:289)
   at Demo.main(Demo.java:21)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 5) out of bounds for length 10
java_lang_stringbuffer.htm
廣告

© . All rights reserved.