Home » Core Java » Character » How to convert Character to String and a String to Character Array in Java

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

How to convert Character to String and a String to Character Array in Java

In this short tutorial we are going to see how to convert  a Character to a String and a String to a StringArray.

In the first Java program we convert a Character to String. Basically there are two ways to do that:

  • the Character.toString(char ) method of the Character class
  • the String.valueOf(char ) method of the String Class.

To be precise the  Character.toString(char ) method internally uses  String.valueOf(char ) method. So you are be better off with String.valueOf(char).

CharToString.java:

package com.javacodegeeks.java.core;

public class CharToString {

	public static void main(String[] args) {

		char ch = 'J';

		String string1 = Character.toString(ch);

		String string2 = String.valueOf(ch);

		System.out.println("character is : " + ch
				+ ". String using String.valueOf(char c):  " + string2);

		System.out.println("character is : " + ch
				+ ". String using Character.toString(char c):  " + string1);
	}
}

Output:

character is : J. String using String.valueOf(char c): J character is : J. String using Character.toString(char c): J

Now we are going to use String.toCharArray() to convert a string into an array of characters.

StringToCharArray.java:

package com.javacodegeeks.java.core;

import java.util.Arrays;

public class StringToCharArray {

	public static void main(String[] args) {

		String string = "JavaCodeGeeks";

		char[] charArray = string.toCharArray();

		System.out.println("String is:" + string + " Character Array : "
				+ Arrays.toString(charArray));
	}
}

As you can see I am using Arrays.toString(char[]) in order to print the character array in a more readable way. So the output will be:

String is:JavaCodeGeeks Character Array : [J, a, v, a, C, o, d, e, G, e, e, k, s]

 
This wan an example of how to convert Character to String and a String to Character Array in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns