Java Tutorial Java Advanced Java References

Java String - toCharArray() Method



The Java toCharArray() method returns a character array containing all characters of the string.

Syntax

public char[] toCharArray()

Parameters

No parameter is required.

Return Value

Returns a character array containing all characters of the string.

Example:

In the example below, toCharArray() method returns a character array containing all characters of the given string.

public class MyClass {
  public static void main(String[] args) {
    String MyString = "HELLO";
    char[] Arr = MyString.toCharArray();
    
    for(int i = 0; i < Arr.length; i++)
      System.out.print(Arr[i] + " ");
  }
}

The output of the above code will be:

H E L L O

❮ Java String Methods