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

This Java program converts the character array to a string using the copyValueOf function.

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

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

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