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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/string/string_tochararray.php