Java StringBuilder.getChars() – Examples
In this tutorial, we will learn about the Java StringBuilder.getChars() function, and learn how to use this function to copy characters from this StringBuilder sequence to a destination array, in the specified source index range, with the help of examples.
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
StringBuilder.getChars() copies characters from this sequence, which start at index srcBegin
until srcEnd
, into the destination character array dst
. The characters are copied to dst
array, from the offset of dstBegin
in dst
array.
Syntax
The syntax of getChars() function is
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
where
Parameter | Description |
---|---|
srcBegin | The index from which copying to destination array starts. |
srcEnd | The index at which copying to destination array stops. |
dst | The array to which characters have to be copied. |
dstBegin | The index in destination array, from which copy has to be done. |
Returns
The function returns void.
Example 1 – getChars(srcBegin, srcEnd, dst, dstBegin)
In this example, we will initialize a StringBuilder object with a string literal, and copy the chars in index range [3, 7) to another char array.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
char[] dst = new char[11];
int srcBegin = 3;
int srcEnd = 7;
int dstBegin = 0;
stringBuilder.getChars(srcBegin, srcEnd, dst, dstBegin);
System.out.print("Destination char array is : ");
System.out.print(dst);
}
}
Output
Destination char array is : defg
Example 2 – getChars() – srcBegin is negative
In this example, we will give a negative value for srcBegin
. Since, the index is out of range for StringBuilder sequence, getChars() throws java.lang.StringIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
char[] dst = new char[11];
int srcBegin = -3;
int srcEnd = 7;
int dstBegin = 0;
stringBuilder.getChars(srcBegin, srcEnd, dst, dstBegin);
System.out.print("Destination char array is : ");
System.out.print(dst);
}
}
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start -3, end 7, length 14
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.getChars(Unknown Source)
at java.base/java.lang.StringBuilder.getChars(Unknown Source)
at Example.main(Example.java:9)
Example 3 – getChars() – dstBegin is negative
In this example, we will give a negative value for dstBegin
. Since, the index is out of range for destination array dst
, getChars() throws java.lang.StringIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
char[] dst = new char[11];
int srcBegin = 3;
int srcEnd = 7;
int dstBegin = -2;
stringBuilder.getChars(srcBegin, srcEnd, dst, dstBegin);
System.out.print("Destination char array is : ");
System.out.print(dst);
}
}
Output
Exception in thread "main" java.lang.IndexOutOfBoundsException: start -2, end 2, length 11
at java.base/java.lang.AbstractStringBuilder.checkRange(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.getChars(Unknown Source)
at java.base/java.lang.StringBuilder.getChars(Unknown Source)
at Example.main(Example.java:9)
Example 4 – getChars() – srcBegin > srcEnd
In this example, we will give a value for srcBegin
such that it is greater than srcEnd
. Since, the prerequisite is that srcBegin >= srcEnd
, getChars() throws java.lang.StringIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
char[] dst = new char[11];
int srcBegin = 7;
int srcEnd = 3;
int dstBegin = 0;
stringBuilder.getChars(srcBegin, srcEnd, dst, dstBegin);
System.out.print("Destination char array is : ");
System.out.print(dst);
}
}
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 7, end 3, length 14
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.getChars(Unknown Source)
at java.base/java.lang.StringBuilder.getChars(Unknown Source)
at Example.main(Example.java:9)
Example 5 – getChars() – srcEnd > this.length
In this example, we will give a value for srcBegin
such that it is greater than the length of this StringBuilder sequence. Since srcBegin
is out of the sequence index range, getChars() throws java.lang.StringIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
char[] dst = new char[11];
int srcBegin = 71;
int srcEnd = 73;
int dstBegin = 0;
stringBuilder.getChars(srcBegin, srcEnd, dst, dstBegin);
System.out.print("Destination char array is : ");
System.out.print(dst);
}
}
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 71, end 73, length 14
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.getChars(Unknown Source)
at java.base/java.lang.StringBuilder.getChars(Unknown Source)
at Example.main(Example.java:9)
Example 6 – getChars() – dst do not have enough length
In this example, we will choose the argument values for getChars() method such that dstBegin+srcEnd-srcBegin
is greater than dst.length
. Since we do not have enough space in the destination to copy the chars from source StringBuilder sequence, getChars() throws java.lang.StringIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
char[] dst = new char[2];
int srcBegin = 3;
int srcEnd = 7;
int dstBegin = 0;
stringBuilder.getChars(srcBegin, srcEnd, dst, dstBegin);
System.out.print("Destination char array is : ");
System.out.print(dst);
}
}
Output
Exception in thread "main" java.lang.IndexOutOfBoundsException: start 0, end 4, length 2
at java.base/java.lang.AbstractStringBuilder.checkRange(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.getChars(Unknown Source)
at java.base/java.lang.StringBuilder.getChars(Unknown Source)
at Example.main(Example.java:9)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java StringBuilder.getChars() function, and also learnt how to use this function with the help of examples.