SoFunction
Updated on 2025-04-04

Three ways to convert String to char array in Java

Method 1: Use toCharArray()

The toCharArray() method is the most straightforward method, which can convert strings into character arrays.

public class StringToCharArrayExample {
    public static void main(String[] args) {
        String str = "Hello";
        char[] charArray = (); // Convert String to char array        
        // Output char array        for (char c : charArray) {
            (c + " ");
        }
    }
}

Output:

H e l l o 

Method 2: Use charAt() and loop

You can use the charAt() method to get the characters in the String one by one in a loop and then manually fill the character array.

public class StringToCharArrayExample {
    public static void main(String[] args) {
        String str = "Hello";
        char[] charArray = new char[()]; // Create a char array with the same length as the String        
        for (int i = 0; i < (); i++) {
            charArray[i] = (i); // Use charAt to get each character and fill the array        }
        
        // Output char array        for (char c : charArray) {
            (c + " ");
        }
    }
}

Output:

H e l l o 

Method 3: Use getChars() method

The getChars() method can copy characters of a specified range from a String into a character array.

public class StringToCharArrayExample {
    public static void main(String[] args) {
        String str = "Hello";
        char[] charArray = new char[()]; // Create a char array with the same length as the String        
        (0, (), charArray, 0); // Copy the string's characters into the char array        
        // Output char array        for (char c : charArray) {
            (c + " ");
        }
    }
}

Output:

H e l l o 

Summarize

  • toCharArray(): The most direct way to convert the entire string into a character array.
  • charAt() + loop: Get each character manually and put it into an array.
  • getChars(): Copy characters in the specified range into the character array.

Attachment: char array to string

There are roughly 6 ways to convert char to String. The summary is as follows:

1. String s = ('c'); //The most efficient method

2. String s = (new char[]{'c'}); //Convert a char array to String

3. String s = ('c');

// The (char) method actually returns (char)

4. String s = new Character('c').toString();

5. String s = "" + 'c';

// Although this method is simple, it is the least efficient method
// The value of String Object in Java is actually immutable and is a final variable.
// So every time we make any changes to the String, we initialize a brand new String Object and point the original variable to this new String.
// Java overloads the method of using the + operator to handle String addition.
// The strings are added directly to the connection actually call the following method:
// new StringBuilder().append("").append('c').toString();

6. String s = new String(new char[]{'c'});

This is the end of this article about three ways to convert String into char arrays in Java. For more related Java to convert String into char arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!