Java String getchars() Method: The Complete Guide

0
9
Java String getchars() Method Example Tutorial

The Java String getchars() method is used to copy String characters to an array of chars.

Java String getchars()

Java String getchars() is a built-in method used to copy the characters from the given string into a destination character array. The getChars() String class method generally copies characters from this string to the target character array object.

Syntax

public void getChars(int StartIndex, int EndIndex, char[] destArray, int destStartIndex);

Arguments

  1. StartIndex – Refers index of the first character in the string to be copied.
  2. EndIndex – Refers to the index of the last character in the string to be copied.
  3. destArray: Refers to the destination array where the characters should be copied.
  4. destStartIndex: Refers to the index in the array starting from where the chars will be pushed into the array.

Return Type

This method does not return any value.

Exception: This method throws an exception called IndexOutOfBoundsException if any of the following reason occurs:

  1. StartIndex is less than 0.
  2. EndIndex is greater than the string length.
  3. destArray value is negative.
  4. destStartIndex is larger than array length.

See the following programs.

class Getchar {
    public static void main(String args[]) {
        String str = "Welcome to Appdivend";
        char[] destArray = new char[20];
        try {
            // StartIndex is 11 and EndIndex is 20
            str.getChars(11, 20, destArray, 0);
            System.out.println(destArray);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

See the output.

Java String getchars() Method Example

Programming Example on Exception Handling

See the following code.

class Getchar {
    public static void main(String args[]) {
        String str = "Welcome to Appdivend";
        char[] destArray = new char[20];
        try {
            // providing EndIndex value larger
            str.getChars(12, 30, destArray, 0);
            System.out.println(destArray);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

See the output.

➜  java javac Getchar.java
➜  java java Getchar
java.lang.StringIndexOutOfBoundsException: String index out of range: 30
➜  java

More Examples of Java String getchars() Method

See the following code.

import java.util.Arrays;

public class Getchar {
    public static void main(String[] args) {
        String source = "This is Upside Down";
        char[] target = new char[15];
        target[0] = '*';
        target[1] = '/';
        source.getChars(5, source.length(), target, 2);
        System.out.println(Arrays.toString(target));
    }
}

See the output.

➜  java javac Getchar.java
➜  java java Getchar
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.lang.String.getChars(String.java:826)
	at Getchar.main(Getchar.java:9)
➜  java

The above example source code demonstrates the use of the getchars() method of the String class.

The example also used the length() method of the String class, which generally gets how many characters the source string is.

The length has been used as the endIndex parameter, which means we get the characters starting from the startIndex up to the end of the source String. We have also instantiated a new character array for the target char as our getChars() method parameter.

That’s it for this article.

Recommended Posts

Java String Equals

Java String endsWith() Function

Java String Contains

Java String Concat()

Java String charAt()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.