Menu
Topics Index
...
`

StringgetChars - Java Example Program


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.

© meritcampus 2019

All Rights Reserved.

Open In App