Building Cloud Expertise with centron - Our Tutorials

Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.

String to Char Conversion in Java

Sometimes we have to convert String to the character array in java programs or convert a string to char from specific index.

String to char Java

String class has three methods related to char. Let’s look at them before we look at a java program to convert string to char array.

  • char[] toCharArray(): This method converts string to character array. The char array size is same as the length of the string.
  • char charAt(int index): This method returns character at specific index of string. This method throws StringIndexOutOfBoundsException if the index argument value is negative or greater than the length of the string.
  • getChars(int srcBegin, int srcEnd, char dst[], int dstBegin): This is a very useful method when you want to convert part of string to character array. First two parameters define the start and end index of the string; the last character to be copied is at index srcEnd-1. The characters are copied into the char array starting at index dstBegin and ending at dstBegin + (srcEnd-srcBegin) – 1.

Let’s look at a simple string to char array java program example.

package com.journaldev.string;

public class StringToCharJava {

    public static void main(String[] args) {
        String str = "journaldev";
        //string to char array
        char[] chars = str.toCharArray();
        //char at specific index
        char c = str.charAt(2);
        //Copy string characters to char array
        char[] chars1 = new char[7];
        str.getChars(0, 7, chars1, 0);


In above program, toCharArray and charAt usage is very simple and clear. In getChars example, first 7 characters of str will be copied to chars1 starting from its index 0. That’s all for converting string into char java program.

Experience the Future of Cloud Technology - Start Your Free Trial Now!

Dive into the world of cloud computing with our exclusive free trial. Experience how our cloud solutions can revolutionize your Java development, including 'String to Char' conversions.

Try for free!