Java String toCharArray() method

Let’s learn java String toCharArray() method.

java String toCharArray() method

Java String toCharArray() method

String toCharArray() method converts a string to a new character array.

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.

Also read – java overview

Let’s see java String toCharArray() with example,

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



Output:

Character array:
F
l
o
w
e
r

B
r
a
c
k
e
t
s