StringBuffer getChars() method in Java with Examples (original) (raw)
Last Updated : 17 Nov, 2022
The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuffer class copies the characters starting at the index:srcBegin to index:srcEnd-1 from actual sequence into an array of char passed as parameter to function.
- The characters are copied into the array of char dst[] starting at index:dstBegin and ending at index:dstbegin + (srcEnd-srcBegin) – 1.
- The first character to be copied to array is at index srcBegin and the last character to be copied is at index srcEnd-1.
- The total number of characters to be copied is equal to srcEnd-srcBegin.
Syntax:
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Parameters: This method takes four parameters:
- srcBegin: int value represents index on which the copying is to be started.
- srcEnd: int value represents index on which the copying is to be stopped.
- dst: array of char represents the array to copy the data into.
- dstBegin: int value represents index of dst array to start pasting the copied data.
Returns: This method returns nothing. Exception: This method throws StringIndexOutOfBoundsException if:
- srcBegin < 0
- dstBegin < 0
- srcBegin > srcEnd
- srcEnd > this.length()
- dstBegin+srcEnd-srcBegin > dst.length
Below programs demonstrate the getChars() method of StringBuffer Class: Example 1:
Java
import
java.util.*;
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` StringBuffer
`` str
`` =
new
StringBuffer("Geeks For Geeks");
`` System.out.println("String = "
`` + str.toString());
`` char
[] array =
new
char
[
15
];
`` Arrays.fill(array,
'.'
);
`` str.getChars(
0
,
9
, array,
1
);
`` System.out.print("
char
array contains : ");
`` for
(
int
i =
0
; i < array.length; i++) {
`` System.out.print(array[i] + " ");
`` }
`` }
}
Output:
String = Geeks For Geeks char array contains : . G e e k s F o r . . . . .
Example 2: To demonstrate StringIndexOutOfBoundsException.
Java
import
java.util.*;
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` StringBuffer
`` str
`` =
new
StringBuffer("Contribute Geeks");
`` char
[] array =
new
char
[
10
];
`` Arrays.fill(array,
'$'
);
`` try
{
`` str.getChars(
2
,
1
, array,
0
);
`` }
`` catch
(Exception e) {
`` System.out.println("Exception: " + e);
`` }
`` }
}
Output:
Exception: java.lang.StringIndexOutOfBoundsException: srcBegin > srcEnd
References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#getChars(int, int, char%5B%5D, int)