java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to convert string to char array?

Here we have a small class that convert a string literal into array, a character array.

package com.javacoderanch.example.lang;

public class StringToArrayExample
{
    public static void main(String[] args)
    {
        // We have a string literal that contains the moto of this website.
        String literal = "Java Code Ranch - Learn Java Programming by Examples";
        
        // Now we want to convert or devided it into a small array of char. To
        // do this we can simply used String.toCharArray() method. This method
        // splits the string into an array of characters.
        char[] temp = literal.toCharArray();
        
        // Here we just iterate the char array and print it to our console.
        for (int i = 0; i < temp.length; i++)
        {
            System.out.print(temp[i]);
        }
        
    }
}