Write a Java program to get the contents of a given string as a character array.

Java Programming Language String in java (Article) String in java (Program)

17973

Program:

public class Exercise {

  public static void main(String[] args)
    {
        String str = "This is a sample string.";

        // Copy the contents of the String to a byte array.
        // Only copy characters 4 through 10 from str.
        // Fill the source array starting at position 2.
        char[] arr = new char[] { ' ', ' ', ' ', ' ',
                                  ' ', ' ', ' ', ' ' };
        str.getChars(4, 10, arr, 2);

        // Display the contents of the byte array.
        System.out.println("The char array equals \"" +
            arr + "\"");
    }
}

Output:

The char array equals "[C@15db9742"
Press any key to continue . . .

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.