Java String toCharArray() Method Tutorial

In this section, we will learn what the String toCharArray() method is and how to use it in Java.

What is Java String toCharArray() Method?

The Java String toCharArray() method is used to create an array of characters of a string value.

Note: this method does not change the original string value.

Note: because the content of the target string will be copied into the newly created `char array`, the length of both array and the target string is equal.

Java toCharArray() Method Syntax:

public char[] toCharArray()

toCharArray() Method Parameters:

The method does not take an argument.

toCharArray() Method Return Value:

The return value of this method is an array of type char that contains the characters of the target string value.

toCharArray() Method Exceptions:

The method does not throw an exception.

Example: using String toCharArray() method

public class Simple {

    public static void main(String[] args)  {
        String s1 = "Be kind to one another";
        char[] chars = s1.toCharArray();
        for(char c: chars){
            System.out.print(c+", ");
        }
    }
}

Output:

B, e, , k, i, n, d, , t, o, , o, n, e, , a, n, o, t, h, e, r,
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies