Open In App
Related Articles

Java String toCharArray() with example

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Jun, 2023
Like Article
Save Article
Similar Reads
Related Tutorials