How to convert string to char array in java
In Java, you can convert a string to a char array by using the
toCharArray() method like this: char[] chars = yourString.toCharArray();.Examples
Inputhello
Output['h', 'e', 'l', 'l', 'o']
InputJava123
Output['J', 'a', 'v', 'a', '1', '2', '3']
Input
Output[]
How to Think About It
To convert a string to a char array, think of the string as a sequence of letters and symbols. You want to split this sequence into individual characters stored in an array. Java provides a simple method called
toCharArray() that does this splitting automatically.Algorithm
1
Take the input string.2
Call the <code>toCharArray()</code> method on the string.3
Store the returned char array.4
Use or return the char array as needed.Code
java
public class Main { public static void main(String[] args) { String text = "hello"; char[] chars = text.toCharArray(); System.out.print("Char array: ["); for (int i = 0; i < chars.length; i++) { System.out.print('\'' + String.valueOf(chars[i]) + '\''); if (i < chars.length - 1) System.out.print(", "); } System.out.println("]"); } }
Output
Char array: ['h', 'e', 'l', 'l', 'o']
Dry Run
Let's trace converting the string "hello" to a char array.
1
Input string
text = "hello"
2
Call toCharArray()
chars = text.toCharArray() -> ['h', 'e', 'l', 'l', 'o']
3
Print char array
Output: ['h', 'e', 'l', 'l', 'o']
| Index | Character |
|---|---|
| 0 | h |
| 1 | e |
| 2 | l |
| 3 | l |
| 4 | o |
Why This Works
Step 1: String is a sequence of characters
A string in Java holds characters in order, like beads on a string.
Step 2: toCharArray() splits string
The toCharArray() method breaks the string into individual characters and puts them in an array.
Step 3: Array holds characters separately
The resulting char array stores each character separately, allowing easy access and manipulation.
Alternative Approaches
Using a loop and charAt()
java
public class Main { public static void main(String[] args) { String text = "hello"; char[] chars = new char[text.length()]; for (int i = 0; i < text.length(); i++) { chars[i] = text.charAt(i); } System.out.println(java.util.Arrays.toString(chars)); } }
This method manually copies each character but is longer and less direct than toCharArray().
Using String.getBytes() and casting
java
public class Main { public static void main(String[] args) { String text = "hello"; byte[] bytes = text.getBytes(); char[] chars = new char[bytes.length]; for (int i = 0; i < bytes.length; i++) { chars[i] = (char) bytes[i]; } System.out.println(java.util.Arrays.toString(chars)); } }
This converts bytes to chars but may cause issues with non-ASCII characters.
Complexity: O(n) time, O(n) space
Time Complexity
The method processes each character once, so time grows linearly with string length.
Space Complexity
A new char array of the same length as the string is created, so space grows linearly.
Which Approach is Fastest?
toCharArray() is the fastest and simplest; manual loops add overhead and complexity.
| Approach | Time | Space | Best For |
|---|---|---|---|
| toCharArray() | O(n) | O(n) | Simple and direct conversion |
| Loop with charAt() | O(n) | O(n) | Manual control over conversion |
| getBytes() and cast | O(n) | O(n) | Works only for ASCII, less safe |
Use
toCharArray() for a simple and efficient conversion from string to char array.Trying to cast a string directly to a char array instead of using
toCharArray().