Friday, October 3, 2025
HomeLanguagesJavaJava String toCharArray() with example

Java String toCharArray() with example

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 = "Lazyroar";
        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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32332 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6819 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS