How to Convert Character Array to String in Java?
Last Updated :
20 Dec, 2024
Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character "\0". A character array can be converted to a string and vice versa. In this article, we will discuss how to convert a character array to a string.
Examples:
Input 1 : char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's' }
Output 1 : "geeksforgeeks"
Input 2 : char s[] = { 'c', 'o', 'd', 'i', 'n', 'g' }
Output 2 : "coding"
Program:
Java
// Convert Character Array to String
// Using copyOf() method ofArrays() Class
import java.util.*;
class GFG {
// To convert a character
// array to a string using the constructor
public static String toString(char[] a){
String str = new String(a);
return str;
}
public static void main(String args[])
{
char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' };
System.out.println(toString(s));
}
}
Here we are using copyOf() method of Array class . The given character can be passed into the String constructor. By default, the character array contents are copied using the Arrays.copyOf() method present in the Arrays class.
Other Methods to Convert Character Array to String
- Using StringBuilder class
- Using valueOf() method of String class
- Using copyValueOf() method of String class
- Using Collectors in Streams
1. Using StringBuilder class
Another way to convert a character array to a string is to use the StringBuilder class. Since a StringBuilder is a mutable class, therefore, the idea is to iterate through the character array and append each character at the end of the string. Finally, the string contains the string form of the characters.
Example:
Java
// Convert Character Array to String
// Using StringBuilder Class
import java.util.*;
public class GFG {
// To convert a character array to a string
// using the StringBuilder class
public static String toString(char[] a){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.length; i++)
sb.append(a[i]);
return sb.toString();
}
public static void main(String args[]){
char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' };
System.out.println(toString(s));
}
}
2. Using valueOf() method of String class
Another way to convert a character array to a string is to use the valueOf() method present in the String class. This method inherently converts the character array to a format where the entire value of the characters present in the array is displayed. This method generally converts int, float, double, char, boolean, and even object to a string. Here we will achieve the goal by converting our character array to string.
Example:
Java
// Convert Character Array to String
// Using valueOf() method
import java.util.*;
class GFG {
// To convert a character array to string
// using the valueOf() method
public static String toString(char[] a)
{
String string = String.valueOf(a);
return string;
}
// Main driver method
public static void main(String args[])
{
char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' };
System.out.println(toString(s));
}
}
3. Using copyValueOf() method of String class
Using copyValueOf() method of String class to use contents from the character array that are copied and subsequently modified without affecting the string to be returned, hence this method also enables us to convert the character array to a string which can be perceived even better from the example provided below as follows.
Example:
Java
// Convert Character Array to String
// Using copyValueOf() method
import java.util.*;
class GFG {
public static void main(String[] args)
{
char[] arr = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' };
// Storing it in a string
// using copyValueOf() over string
String str = String.copyValueOf(arr);
System.out.print(str);
}
}
4. Using Collectors in Streams
With the introduction of streams in java8, we straight away use Collectors in streams to modify our character input array elements and later uses joining() method and return a single string and print it.
Example:
Java
// Convert a Character array to String
// Using Collectors in Streams in Java8
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Main {
public static void main(String[] args)
{
char[] charr = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' };
// Using collectors to collect array elements and
// later using joining method to return a single
// string
String str = Stream.of(charr)
.map(arr -> new String(arr))
.collect(Collectors.joining());
System.out.println(str);
}
}
Similar Reads
Java Program to convert Character Array to IntStream Given a Character Array, the task is to convert this array into an IntStream containing the ASCII values of the character elements. Examples: Input: char[] = { 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115 Input: char[] = { 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' }
1 min read
Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character
3 min read
Convert List of Characters to String in Java Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are
4 min read
Program to convert Byte Array to Writer in Java References: Writer Class Approach: Writer class is used to write character stream, by which byte array can be passed as an argument. By this way, byte array can be converted into Writer class. To get the byte array from String, getBytes() method is used. Below is the implementation of the above appr
2 min read
Java Program to Get a Character from a String Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the speci
5 min read
Convert byte[] array to File using Java As we know whenever it comes to writing over a file, write() method of the File class comes into play but here we can not use it in order to convert that byte into a file. In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementatio
3 min read
String Array with Enhanced For Loop in Java Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence it is mainly used for traversing arrays or collections.
1 min read
Character Array in Java In Java, a character array is a data structure used to store a sequence of characters. The characters are stored in contiguous memory locations and can be accessed by their index, similar to an array of integers or any other data type. Declaring a Character Array A character array can be declared in
5 min read
Difference between String and Character array in Java Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays:PropertyStringCharacter ArrayDefinitionA sequence of characters is r
3 min read
Convert a String into a square matrix grid of characters Given a string of length L. The task is to convert the string into a grid.Examples: Input : str = "haveaniceday" Output : have anic eday Explanation: k is the separator. If k is 4 then the output will be "have anic eday" Input :str = "geeksforgeeks" Output : geek sfor geek s Note: & l = length o
5 min read