Char array to string Java
In this post, you will learn how to convert a char array to a string using the Java programming language. A character array can be converted to a string and vice versa.
The Java char keyword is a primitive data type. It is used to declare the character-type variables and methods. Java array is an object that contains elements of a similar data type. Additionally, the elements of an array are stored in a contiguous memory location.
In Java, string is basically an object that represents a sequence of char values. For example, "welcome" is a string containing a sequence of characters 'w' , 'e' , 'l' , 'c' , 'o' , 'm' and 'e'. There are different ways to convert a char array to string in Java. Here, we have mentioned most of them.
Using StringBuilder class
The StringBuilder in Java represents a mutable sequence of characters. The append() method of the StringBuilder class appends the string representation of the char[] array. In the given example, we iterate through the character array and append each character at the end of the string. At last, the string contains the string form of the characters.
import java.util.*;
public class CharArrayString {
// Convert a character array to a string
// using the StringBuilder class
public static String toString(char[] a)
{
// Creating object of StringBuilder class
StringBuilder sb = new StringBuilder();
// Creating a string using append() method
for (int i = 0; i < a.length; i++) {
sb.append(a[i]);
}
return sb.toString();
}
// Main driver method
public static void main(String args[])
{
char arr[] = {'e', 't', 'u',
't', 'o', 'r','i', 'a', 'l',
's', 'p', 'o', 'i', 'n', 't' };
// Displaying the string
System.out.println(toString(arr));
}
}
Output of the above code:
etutorialspoint
Using valueOf() method of String class
The Java string valueOf() method converts different types of values into string. With the help of the string valueOf() method, we can convert char array to string. The method parses a char[] array as a parameter. It returns a newly allocated string that represents the same sequence of characters contained in the character array.
import java.util.*;
public class CharArrayString {
// Convert a character array to a string
// using the valueOf() method
public static String toString(char[] arr)
{
// Creating an object of String class
String str = String.valueOf(arr);
return str;
}
// Main driver method
public static void main(String args[])
{
char arr[] = {'e', 't', 'u',
't', 'o', 'r','i', 'a', 'l',
's', 'p', 'o', 'i', 'n', 't' };
// Displaying the string
System.out.println(toString(arr));
}
}
Output of the above code:
etutorialspoint
Using copyValueOf() method of String class
The copyValueOf() method returns a string that represents the characters of a char array. This method returns a new string array and copies the characters into it.
// Importing String class
import java.util.*;
// Main class
public class CharArrayString {
// Driver method
public static void main(String[] args) {
// Customly declaring an input character array
char arr[] = {'e', 't', 'u',
't', 'o', 'r','i', 'a', 'l',
's', 'p', 'o', 'i', 'n', 't' };
// Using copyValueOf() over string
String str = String.copyValueOf(arr);
// Printing character array
System.out.print(str);
}
}
Output of the above code:
etutorialspoint
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java