# How to convert a string to a char array in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Oct 30, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-convert-string-to-char-array

> Learn how to use the String. toCharArray() method to convert a string into an array of characters in Java.

Java provides several ways to convert a string into an array of characters (`char[]`). You can use the `String.toCharArray()` or `String.chars()` method to convert a string into a characters array.

## Convert a string to a `char[]` using `String.toCharArray()`

The `String.toCharArray()` method returns an array of characters from the given string. The returned character array has the same length as the length of the string.

Here is an example:

```java
// Declare a string
String str = "Java Protips";

// Convert string to a char array
char[] chars = str.toCharArray();

System.out.println(Arrays.toString(chars));
// [J, a, v, a,  , P, r, o, t, i, p, s]
```

## Convert a string to a `char[]` using `String.chars()`

The `String.chars()` is yet another simple method to transform a string into an array of characters in Java 8 and higher, as shown below:

```java
// Declare a string
String str = "Java Protips";

// Convert string to a char array
Character[] chars = str.chars()
        .mapToObj(c -> (char) c)
        .toArray(Character[]::new);

System.out.println(Arrays.toString(chars));
// [J, a, v, a,  , P, r, o, t, i, p, s]
```

Read [this article](https://attacomsian.com/blog/java-convert-char-array-to-string) if you want to convert a character array to a string in Java.
