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); } }
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
This Program helps to convert char array to string using the valueOf function.
package NumPrograms; public class CharArraytoString3 { 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.valueOf(ch); System.out.println(chToStr); } }
Tutorial Gateway
Tutorial Gateway
Program to Convert Char 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.Please refer to the Convert Character to Int and Convert Character to String articles.
public class Example { public static void main(String[] args) { char ch[] = {'L', 'e', 'a', 'r', 'n', ' ', 'P', 'r', 'o', 'g', 'r', 'a','m','s'}; 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 Programs