Java String toCharArray() method

Let’s learn java String toCharArray() method.

Java String toCharArray() method

Java String toCharArray() method converts given string into new character array.

java String toCharArray() method

Syntax

public char[] toCharArray()

Return

returns 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.

Let’s see java String toCharArray() method example or string to char array java.

public class StringToCharArrayDemo
{
   public static void main(String[] args)
   {
      String strInput = new String("Flower Brackets");
      // using toCharArray() method java
      char[] chArray = strInput.toCharArray();
      System.out.println("Character array: ");
      for(char ch : chArray)
      {
         System.out.println(ch);
      }
   }
}


Output:

Character array:
F
l
o
w
e
r

B
r
a
c
k
e
t
s


Also read – major features of java