AppDividend
Latest Code Tutorials

Java String getchars() Method Example

0

In this tutorial, we will see Java String getchars() Method Example. Java String getChars() method is used to copy the characters from the given string into a destination character array. The getChars() method of String class generally copies character from this string to the target character array object.

Java String getchars()

The method getChars() is used for copying String characters to an array of chars.

Syntax

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

Here,

  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 String class.

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

The length has been used as the endIndex parameter, which means we are getting 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 a parameter of our getChars() method.

Finally, Java String getchars() Method Example is over.

Recommended Posts

Java String Equals Example

Java String endsWith() Function Example

Java String Contains Example

Java String Concat() Method

Java String charAt() Method

Leave A Reply

Your email address will not be published.

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