⬅ Previous Topic
Java StringgetBytes()
methodNext Topic ⮕
Java StringhashCode()
methodgetChars()
method⬅ Previous Topic
Java StringgetBytes()
methodNext Topic ⮕
Java StringhashCode()
methodThe getChars()
method in Java allows you to copy a specific portion of a string into a character array. Think of it as cutting out a piece of your string and pasting it somewhere else.
public void getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
Let's break down what each parameter does:
dst
where you want to start placing the copied characters.Parameter | Description |
---|---|
srcBegin | Starting index of the substring to be copied. |
srcEnd | Ending index (exclusive) of the substring to be copied. |
dst | Destination character array. |
dstBegin | Starting index in the destination array. |
The getChars()
method is a void method, meaning it doesn't return any value. Its purpose is to modify the character array you provide.
This example copies the characters from index 0 up to (but not including) index 5 of a string into a char array, starting at index 0 of the array.
public class GetCharsExample1 {
public static void main(String[] args) {
String str = "Hello World";
char[] charArray = new char[5];
str.getChars(0, 5, charArray, 0);
System.out.print("Copied characters: ");
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
System.out.println();
}
}
Copied characters: Hello
Explanation: We're taking the substring from index 0 (inclusive) to 5 (exclusive), which is "Hello". This substring is then copied into charArray
starting at index 0. The loop prints the contents of `charArray`.
This example copies a portion of a string, but starts pasting it into the destination array at an offset.
public class GetCharsExample2 {
public static void main(String[] args) {
String str = "Java is fun";
char[] charArray = new char[10];
str.getChars(5, 9, charArray, 2);
System.out.print("Copied characters: ");
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
System.out.println();
}
}
Copied characters: isfu
Explanation: We're copying the substring from index 5 (inclusive) to 9 (exclusive) of "Java is fun", which is "is f". This gets pasted into charArray
, starting at index 2. Notice how `charArray` has a length of 10 to accommodate this offset. The first two elements are skipped.
This example demonstrates copying when the substring's length is smaller than the destination array’s available space from its start index.
public class GetCharsExample3 {
public static void main(String[] args) {
String str = "Short";
char[] charArray = new char[15];
str.getChars(0, 2, charArray, 5);
System.out.print("Copied characters: ");
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
System.out.println();
}
}
Copied characters: Sh
Explanation: We're copying "Sh" from index 0 to 2 of the string 'Short', and placing it into charArray at index 5. The remaining elements in charArray (before index 5) will retain their existing values or default initialization if they haven’t been assigned, as seen in the output.
This example demonstrates what happens when the `srcEnd` is beyond the string's length. It doesn't throw an exception but copies up to the end of the string.
public class GetCharsExample4 {
public static void main(String[] args) {
String str = "Hello";
char[] charArray = new char[5];
str.getChars(1, 10, charArray, 0);
System.out.print("Copied characters: ");
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
System.out.println();
}
}
Copied characters: ello
Explanation: We attempt to copy from index 1 up to (but not including) index 10. Since the string's length is only 5, the copying stops at the end of the string, effectively copying "ello". It is important to handle potential out-of-bounds situations in your code.
⬅ Previous Topic
Java StringgetBytes()
methodNext Topic ⮕
Java StringhashCode()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.