By Pankaj Kumar
Sometimes we have to convert String to the character array in java programs or convert a string to char from specific index.
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();
System.out.println(chars.length);
//char at specific index
char c = str.charAt(2);
System.out.println(c);
//Copy string characters to char array
char[] chars1 = new char[7];
str.getChars(0, 7, chars1, 0);
System.out.println(chars1);
}
}
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.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
I just looked the String interview questions, I am getting soo interesting to study the hole tutorial. Its Nice.
- Naresh
… //Copy string characters to char array str.getChars(chars1, 0); This overloaded method might just work as well?
- Victr Yao
If a String value is “10”, toCharArray returns value 01 instead of the 10…any reason for this?
- Mahesh
what are the member of class ? Is methods and data variable are the member of a class?
- M.king dashti
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.