Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String getChars() method
Syntax and Examples



The 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.

Syntax

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

Parameters

Let's break down what each parameter does:

  • srcBegin: The index in the original string where you want to start copying characters.
  • srcEnd: The index after the last character you want to copy from the original string. This means the copied sequence will go up to, but not include, this index.
  • dst: The character array where you want to paste the copied characters.
  • dstBegin: The index in dst where you want to start placing the copied characters.
ParameterDescription
srcBeginStarting index of the substring to be copied.
srcEndEnding index (exclusive) of the substring to be copied.
dstDestination character array.
dstBeginStarting index in the destination array.

Return Value

The getChars() method is a void method, meaning it doesn't return any value. Its purpose is to modify the character array you provide.

Examples

Example 1: Basic Copy

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`.

Example 2: Copying with Offset

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.

Example 3: Copying with Different Lengths

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.

Example 4: Handling Out-of-Bounds

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.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You 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.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M