Menu
Topics Index
...
`


Strings > Character Extraction >
Siva Nookala - 15 Apr 2016
The getChars() method copies characters from the string into the destination character array.

Syntax of getChars() method is:
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
This method copies characters from index srcBegin to (srcEnd - 1) to the subarray of dst starting at the index dstBegin. The number of characters that are copied to the character array is given by (srcEnd - srcBegin).

Here is a code snippet which demonstrates this method:
StringgetChars
class StringGetCharsDemo
{
    public static void main(String arg[])
    {
        String source = "Merit Campus";
        char[] dest = new char[20];
        dest[5] = 'o';
        dest[6] = 'r';
        dest[7] = 'i';
        dest[8] = 'o';
        dest[9] = 'u';
        dest[10] = 's';
        try {
            System.out.println("dest array contents:" + String.valueOf(dest) );
            source.getChars( 0, 5, dest, 0 ); // LINE A
            System.out.println( dest );
        } catch ( Exception ex ) {
            System.out.println( "An exception occured!" );
        }    
    }
}
OUTPUT

dest array contents:     orious
Meritorious

DESCRIPTION

At LINE A the getChars method copies 5 characters from source to dst array from index 0.

THINGS TO TRY
  • Try with an empty string
  • Try with different index values such as srcEnd < srcBegin, srcEnd = srcBegin.
  • Try copying more characters than the size of the char array.

Dependent Topics : Java String   Learn Arrays And Loops 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App