Java.lang.StringBuffer.getChars() Method

Advertisements


Description

The java.lang.StringBuffer.getChars() method copy the characters from this sequence into the destination character array dst.
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

Declaration

Following is the declaration for java.lang.StringBuffer.getChars() method

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

Parameters

  • srcBegin -- This start copying at this offset.

  • srcEnd -- This stop 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.

Exception

  • NullPointerException -- if dst is null

  • IndexOutOfBoundsException -- this is thrown if any of the following is true:

srcBegin is negative
dstBegin is negative
the srcBegin argument is greater than the srcEnd argument.
srcEnd is greater than this.length().
dstBegin + srcEnd - srcBegin is greater than dst.length 

Example

The following example shows the usage of java.lang.StringBuffer.getChars() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("java programming");
    System.out.println("buffer = " + buff);
    
    // char array
    char[] chArr = new char[]{'t','u','t','o','r','i','a','l','s'};
    
    // copy the chars from index 5 to index 10 into subarray of chArr
    // the offset into destination subarray is set to 3
    buff.getChars(5, 10, chArr, 3);
    // print character array
    System.out.println(chArr);
  }
}

Let us compile and run the above program, this will produce the following result:

buffer = java programming
tutprogrs



Advertisements
Advertisements