Java StringBuffer getChars() Method



The Java StringBuffer getChars() method copies the characters from this sequence into the destination character array dst. In Java, an array is an object that contains an element of similar data types.

The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd - 1. The total number of characters to be copied is srcEnd - srcBegin. The characters are copied into the subarray of dst starting at index dstBegin and ending at index dstbegin + (srcEnd-srcBegin) – 1.

Syntax

Following is the syntax of the Java StringBuffer getChars() method −

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

Parameters

  • srcBegin − This means starts copying at this offset.
  • srcEnd − This means stops copying at this offset.
  • dst − This is the array to copy the data into.
  • dstBegin − This is the offset into dst.

Return Value

This method does not return any value.

Example

If the destination character array is not null, and indexes values are positive, the getChars() method copies the characters of this sequence into the character array.

In the following program, we are creating an object of the StringBuffer class with the value “Welcome to TutorialsPoint”. Using the getChars() method, we are trying to copy the character's value into the destination array at the srcBegin index 3, srcEnd index 5, and dstBegin index 0.

import java.lang.*;
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] + " ");
      }
   }
}

Output

On executing the above program, it will produce the following result −

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

Example

If the destination character array is null, the getChars() method throws a NullPointerException.

In the following program, we are instantiating the StringBuffer class with the value “Java Programming”. Then, we are creating an array of the character with the null value. Using the getChars() method, we are trying to copy the characters of this sequence into the character array, which contains the null value.

import java.lang.*;
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);
      }
   }
}

Output

Following is the output of the above program −

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

Example

If the given values of srcBegin and the dstBegin are negative, this method throws the IndexOutOfBoundsException.

In the following example, we are creating an object of the StringBuffer class with the value “HelloWorld”. Using the getChars() method, we are trying to copy the characters of this sequence into the character array, at the srcBegin index and dstBegin index -1.

import java.lang.*;
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);
      }
   }
}

Output

The above program, produces the following results −

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
Advertisements