Java Program to Convert Character Array To String

Write a Java program to convert character array to string with examples. In this example, we passed the character array to the string constructor, which will convert the character array to string.

package NumPrograms;

public class CharArraytoString1 {	
	
	public static void main(String[] args) {
			
		char ch[] = {'T', 'u', 't', 'o', 'r', 'a', 'l', ' ', 'G', 'a', 't', 'e', 'w', 'a', 'y'};
			
		String chToStr = new String(ch);
		
		System.out.println("Final String After Converting Charcater Array\n");
		System.out.println(chToStr);
	}
}
Convert Character Array To String

Java program to convert the character array to a string using copyValueOf function

This example uses the string copyValueOf method.

package NumPrograms;

public class CharArraytoString2 {
		
	public static void main(String[] args) {
			
		char ch[] = {'T', 'u', 't', 'o', 'r', 'a', 'l', ' ', 'G', 'a', 't', 'e', 'w', 'a', 'y'};
		
		String chToStr = String.copyValueOf(ch);
		
		System.out.println(chToStr);

	}
}
Tutoral Gateway

Java Program to convert character array to string using the valueOf function

package NumPrograms;

public class CharArraytoString3 {
		
	public static void main(String[] args) {
			
		char ch[] = {'L', 'e', 'a', 'r', 'n', ' ', 'J', 'a', 'v', 'a'};
		
		String chToStr = String.valueOf(ch);
		
		System.out.println(chToStr);

	}
}
Learn Java

Java Program to Convert Character Array To String using StringBuilder

In this example, we used for loop to iterate the character array and appended each character to the string builder. Next, we converted the StringBuilder to a string.

package NumPrograms;

public class CharArraytoString4 {
		
	public static void main(String[] args) {
			
		char ch[] = {'L', 'e', 'a', 'r', 'n', ' ', 'J', 'a', 'v', 'a'};
		
		StringBuilder sb = new StringBuilder();
		
		for(int i = 0; i < ch.length; i++)
		{
			sb.append(ch[i]);
		}
		
		String chToStr = sb.toString();
		
		System.out.println(chToStr);

	}
}
Learn Java

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.