Java String getChars() Method



The Java String getChars() method is, used to copy the characters from this string into the destination character array. In Java, an array is an object that stores similar types of elements.

The getChars() method accepts four different parameters that hold the value of the source begin index, source end index, destination char array, and destination begin index. It throws an exception if the srcBegin value is negative, srcBegin is greater than srcEnd, srcEnd is greater than the string, and so on.

The first character to be copied is at srcBegin index, the last character to be copied is at srcEnd-1 index.

Syntax

Following is the syntax of the Java String getChars() method −

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

Parameters

  • srcBegin − This is the index of the first character in the string to copy.

  • srcEnd − This is the index after the last character in the string to copy.

  • dst − This is the destination array.

  • dstBegin − This is the start offset in the destination array.

Return Value

This method does not return any value.

Example

If the srcBegin value is positive, and the srcEnd value is less than the string length, the getChars() method copies the string characters into the char array.

In the following program, we are instantiating a string class with the value of "Welcome to TutorialsPoint". Using the getChars() method, we are trying to copy the string characters into the char array at the specified srcIndex 5, srcEnd 15, and dstbegin 0.

import java.lang.*;
import java.util.*;
public class GetChars {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("Welcome to TutorialsPoint");
      System.out.println("The given string is: " + str);
      
      // create character array
      char[] chararr = new char[10];
      
      // copies characters from starting and ending index into the destination character array
      
      //initialize the srcBegin, srcEnd, and dstBegin values
      int srcBegin = 5, srcEnd = 15, dstBegin = 0;
      System.out.println("The srcBegin, srcEnd, and dstBegin values are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin);
      
      //using the getChars() method
      str.getChars(srcBegin, srcEnd, chararr, dstBegin);
      
      // print the character array
      System.out.print("The Value of character array : " + Arrays.toString(chararr));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: Welcome to TutorialsPoint
The srcBegin, srcEnd, and dstBegin values are: 5, 15, and 0
The Value of character array : [m, e,  , t, o,  , T, u, t, o]

Example

If the given srcBegin index value is negative, the getChars() method throws a StringIndexOutOfBoundsException.

In the following example, we are creating an object of the string class with the value “Hello World”. Then, we create a char array with size 20. Using the getChars() method, we are trying to copy the string characters from the current string into the char array at the specified srBegin index -1, srcEnd index 5, and dstBegin index 0.

import java.lang.*;
import java.util.Arrays;
public class GetChars {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("Hello World");
         System.out.println("The given string is: " + str);
         
         // create character array
         char[] chararr = new char[20];
         
         // copies characters from starting and ending index into the destination character array
         
         //initialize the srcBegin, srcEnd, and dstBegin index values
         int srcBegin = -1, srcEnd = 5, dstBegin = 0;
         System.out.println("The srcBegin, secEnd, and dstBegin index value are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin );
         
         //using the getChars() method
         str.getChars(srcBegin, srcEnd, chararr, dstBegin);
         
         // print the character array
         System.out.print("The Value of character array : " + Arrays.toString(chararr));
      } catch (StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.print("Exception: "  + e);
      }
   }
}

Output

Following is the output of the above program −

The given string is: Hello World
The srcBegin, secEnd, and dstBegin index value are: -1, 5, and 0
java.lang.StringIndexOutOfBoundsException: begin -1, end 5, length 11
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.getChars(String.java:1676)
	at com.tutorialspoint.String.GetChars.main(GetChars.java:17)
Exception: java.lang.StringIndexOutOfBoundsException: begin -1, end 5, length 11

Example

If the srcBegin value is greater than the srcEnd, this method throws a StringIndexOutOfBoundsException.

IIn this example, we are creating a string with the value “Java Programming”. Using the getChars() method, we are trying to copy the string characters from the current string into the char array at the specified srBegin index 5, srcEnd index 3, and dstBegin index 1. Since the serBegin value is greater than the srEnd, the method will throw an exception.

import java.lang.*;
import java.util.Arrays;
public class GetChars {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("Java Programming");
         System.out.println("The given string is: " + str);
         
         // create character array
         char[] chararr = new char[25];
         
         // copies characters from starting and ending index into the destination character array
         
         //initialize the srcBegin, srcEnd, and dstBegin index values
         int srcBegin = 5, srcEnd = 3, dstBegin = 1;
         System.out.println("The srcBegin, secEnd, and dstBegin index value are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin );
         
         //using the getChars() method
         str.getChars(srcBegin, srcEnd, chararr, dstBegin);
         
         // print the character array
         System.out.print("The Value of character array : " + Arrays.toString(chararr));
      } catch (StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.print("Exception: "  + e);
      }
   }
}

Output

The above program, produces the following output −

The given string is: Java Programming
The srcBegin, secEnd, and dstBegin index value are: 5, 3, and 1
java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.getChars(String.java:1676)
	at com.tutorialspoint.String.GetChars.main(GetChars.java:17)
Exception: java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16

Example

If the srcEnd value is greater than the string length, the getChars() method throws a StringIndexOutOfBoundsException.

In the following program, we are instantiating the string class with the value “TutorialsPoint”. Using the getChars() method, we are trying to copy the string characters from this string into the char array at the specified srBegin index 1, srcEnd index 15, whose value is greater than the string length.

import java.lang.*;
import java.util.Arrays;
public class GetChars {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("TutorialsPoint");
         System.out.println("The given string is: " + str);
         System.out.println("The string length is: " + str.length());
         
         // create character array
         char[] chararr = new char[20];
         
         // copies characters from starting and ending index into the destination character array
         
         //initialize the srcBegin, srcEnd, and dstBegin index values
         int srcBegin = 1, srcEnd = 15, dstBegin = 0;
         System.out.println("The srcBegin, secEnd, and dstBegin index value are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin );
         
         //using the getChars() method
         str.getChars(srcBegin, srcEnd, chararr, dstBegin);
         
         // print the character array
         System.out.print("The Value of character array : " + Arrays.toString(chararr));
      } catch (StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.print("Exception: "  + e);
      }
   }
}

Output

After executing the above program, it generates the following output −

The given string is: TutorialsPoint
The string length is: 14
The srcBegin, secEnd, and dstBegin index value are: 1, 15, and 0
java.lang.StringIndexOutOfBoundsException: begin 1, end 15, length 14
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.getChars(String.java:1676)
	at com.tutorialspoint.String.GetChars.main(GetChars.java:18)
Exception: java.lang.StringIndexOutOfBoundsException: begin 1, end 15, length 14
java_lang_string.htm
Advertisements