Tuesday, January 7, 2025
Google search engine
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 Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments