String to char array java - convert string to char

Published on August 3, 2022


String to char array java - convert string to char

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 to char java, string to char array 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.

  1. char[] toCharArray(): This method converts string to character array. The char array size is same as the length of the string.
  2. 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.
  3. 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 to char array and string to char java program. Reference: API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
DigitalOcean Employee
DigitalOcean Employee badge
May 18, 2020

Why is it that if i convert a string with a space between words, it only recognizes the first word and converts that to a char array. Is there a way around it?

- Morphat

    DigitalOcean Employee
    DigitalOcean Employee badge
    October 7, 2019

    what are the member of class ? Is methods and data variable are the member of a class?

    - M.king dashti

      DigitalOcean Employee
      DigitalOcean Employee badge
      March 17, 2019

      If a String value is “10”, toCharArray returns value 01 instead of the 10…any reason for this?

      - Mahesh

        DigitalOcean Employee
        DigitalOcean Employee badge
        December 29, 2018

        … //Copy string characters to char array str.getChars(chars1, 0); This overloaded method might just work as well?

        - Victr Yao

          DigitalOcean Employee
          DigitalOcean Employee badge
          June 26, 2018

          Does the last of character array is a null or not?

          - JeThou

            DigitalOcean Employee
            DigitalOcean Employee badge
            December 12, 2014

            I just looked the String interview questions, I am getting soo interesting to study the hole tutorial. Its Nice.

            - Naresh

              Try DigitalOcean for free

              Click below to sign up and get $200 of credit to try our products over 60 days!

              Sign up

              Join the Tech Talk
              Success! Thank you! Please check your email for further details.

              Please complete your information!

              Featured on Community

              Get our biweekly newsletter

              Sign up for Infrastructure as a Newsletter.

              Hollie's Hub for Good

              Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

              Become a contributor

              Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

              Welcome to the developer cloud

              DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

              Learn more
              DigitalOcean Cloud Control Panel