w3resource

Java String: toCharArray() Method

public char[] toCharArray()

The toCharArray() method converts a given string to a new character array.

Java Platform: Java SE 8

Syntax:

toCharArray()

Return Value: a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Return Value Type: char

Pictorial presentation of Java String toCharArray() Method

Java String: toCharArray() Method

Example: Java String toCharArray() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args)
    {
        String str = "Python Exercises.";

        // Convert the above string to a char array.
char[] arr = str.toCharArray();
    System.out.println();
        // Display the contents of the char array.
System.out.println(arr);
System.out.println();
    }
}

Output:

Python Exercises.

Java Code Editor:

Previous:substring Method
Next:toLowerCase Method



Follow us on Facebook and Twitter for latest update.