Today we will look into java char to String program. We will also learn how to convert String to a char array.
Before we look into java char to String program, let’s get to the basic difference between them.
We can use String.valueOf(char c)
or Character.toString(char c)
to convert char to string. Below is the example program showing how to use these methods to convert char to string.
public class JavaCharToString {
public static void main(String[] args) {
char c = 'X';
String str = String.valueOf(c);
String str1 = Character.toString(c);
System.out.println(c + " char converted to String using String.valueOf(char c) = " + str);
System.out.println(c + " char converted to String using Character.toString(char c) = " + str1);
}
}
java.lang.Character
is the wrapper class for primitive char data type. Character.toString(char c)
internally calls String.valueOf(char c)
method, so it’s better to use String class function to convert char to String. Output of the above program is shown in below image.
Since String is an array of char, we can convert a string to the char array. String class also has a method to get the char at a specific index. Let’s look at a simple program to convert String to a char array.
import java.util.Arrays;
public class JavaStringToCharArray {
public static void main(String[] args) {
String str = "journaldev.com";
// get char at specific index
char c = str.charAt(0);
// Character array from String
char[] charArray = str.toCharArray();
System.out.println(str + " String index 0 character = " + c);
System.out.println(str + " String converted to character array = " + Arrays.toString(charArray));
}
}
To get the char from a String object, we can use chatAt(int index)
method. Whereas toCharArray()
returns the char array of the String. Here we are using Arrays.toString(char[] ca)
method to print the char array in readable format. Output of the above program is shown below. That’s all for the char to String and String to char array conversion. Reference: Character 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
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.