Java Program to Convert Character Array To String

Write a Java program to convert character array to string with examples. In this Java 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);
	}
}
Java Program to Convert Character Array To String

This Java program converts the character array to a string using the String 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("After Converting the Charcater Array to String\n");
		System.out.println(chToStr);

	}
}
After Converting the Charcater Array to String

Tutoral Gateway

Java program to convert character array to string using the string 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("After Converting the Charcater Array to String\n");
		System.out.println(chToStr);

	}
}
After Converting the Charcater Array to String

Learn Java

In this Java example, we used for loop to iterate character array and appended each character to string builder. Next, we converted the string builder to 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("After Converting the Charcater Array to String\n");
		System.out.println(chToStr);

	}
}
After Converting the Charcater Array to String

Learn Java