The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder class copies the characters starting at the given index:srcBegin to index:srcEnd-1 from String contained by StringBuilder into an array of char passed as parameter to function.
- The characters are copied from StringBuilder into the array dst[] starting at index:dstBegin and ending at index:dstbegin + (srcEnd-srcBegin) – 1.
- The first character to be copied from StringBuilder 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 accepts four different parameters:
- srcBegin: represents index on which we have to start copying.
- srcEnd: represents index on which we have to stop copying.
- dst: represents the array to copy the data into.
- dstBegin: represents index of dest array we start pasting the copied data.
Return Value: This method does not return anything.
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 StringBuilder Class:
Example 1:
class GFG {
public static void main(String[] args)
{
StringBuilder
str
= new StringBuilder("WelcomeGeeks");
System.out.println("String = "
+ str.toString());
char[] array = new char[7];
str.getChars(0, 7, array, 0);
System.out.print("Char array contains : ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
|
Output:
String = WelcomeGeeks
Char array contains : W e l c o m e
Example 2:
import java.util.*;
class GFG {
public static void main(String[] args)
{
StringBuilder
str
= new StringBuilder("evil dead_01");
System.out.println("String = "
+ str.toString());
char[] array = new char[10];
Arrays.fill(array, '_');
str.getChars(5, 9, array, 3);
System.out.print("char array contains : ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
|
Output:
String = evil dead_01
char array contains : _ _ _ d e a d _ _ _
Example 3: To demonstrate StringIndexOutOfBoundException
import java.util.*;
class GFG {
public static void main(String[] args)
{
StringBuilder
str
= new StringBuilder("evil dead_01");
char[] array = new char[10];
Arrays.fill(array, '_');
try {
str.getChars(5, 2, array, 0);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.StringIndexOutOfBoundsException: srcBegin > srcEnd
Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#getChars(int, int, char%5B%5D, int)
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
22 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...