Open In App

Java String toCharArray() with example

Improve
Improve
Like Article
Like
Save
Share
Report

The Java string toCharArray() method converts the given string into a sequence of characters. The returned array length is equal to the length of the string. 

Syntax

public char[] toCharArray();

Return Value

  • It returns a newly allocated character array.

Example of Java String toCharArray()

Example 1:

Java




// Java program to demonstrate
// working of toCharArray() method
 
// Driver Class
class Gfg {
    // main function
    public static void main(String args[])
    {
        String s = "GeeksForGeeks";
        char[] gfg = s.toCharArray();
 
        System.out.println(gfg);
    }
}


Output

GeeksForGeeks

Example 2:

Java




// Java program to demonstrate
// working of toCharArray() method
 
// Driver Class
class Gfg {
      // main function
    public static void main(String args[])
    {
        String s = "GeeksforGeeks";
        char[] gfg = s.toCharArray();
       
        for (int i = 0; i < gfg.length; i++) {
            System.out.println(gfg[i]);
        }
    }
}


Output

G
e
e
k
s
f
o
r
G
e
e
k
s


Last Updated : 02 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads