Convert Char array to string in java
A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string can be transformed from a character array and vice versa. We'll look how to make a character array from a string in this tutorial.
Illustrations
Input 1: char s[] = { ‘j’, ‘a’, ‘v’, ‘a’, ‘T’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’, ‘ ’ ,‘t’, ‘u’, ‘t’ , ‘o’ , ‘r’ , ‘i’ , ‘a’ , ‘l’}
Output 1: “javaTpoint tutorial”
Input 2: char s[] = { ‘j’, ‘a’, ‘v’, ‘a’, ‘ ’ , ‘c’, ‘o’ , ‘d’ , ‘e’ , ‘s’ }
Output 2: “javacodes”
Approaches
- Using the Arrays class's copyOf() function
- Making use of the StringBuilder class
- Using the String class's function valueOf()
- Using the String class's function copyValueOf()
- In Streams, Using Collectors
Let us now go through each of the methods in detail, as well as how to implement them using a Java program.
Approach 1: Using the Arrays class's copyOf() function
The String constructor accepts a character as an argument. The Arrays.copyOf() function in the Arrays class is used by default to copy the contents of the character array.
For instance:
// Converting a Character Array to a String in Java
// Using the Arrays class's copyOf() function
// Importing the necessary classes
import java.util.*;
// Main class
class JTP{
//Approach 1
// Using the function Object() to convert a character array to a string
public static String toString(char[] ch)
{
// constructing a string object
String string = new string (ch);
return string ;
}
// Approach 2
// Method of the main driver
Public static void main(string args[] )
{
// Declaring and initializing input character array
char st[] = { ‘j’ , ‘a’, ‘v’ , ‘a’ , ‘T’ , ‘p’ , ‘o’ , ‘i’ , ‘n’, ‘t’ , ‘ ’ ,‘t’ , ‘u’ , ‘t’ , ‘o’ , ‘r’ , ‘i’ , ‘a’ , ‘l’} ;
// To a character array, print the relevant string.
System.out.println( toString ( st ) );
}
}
Output:
javaTpoint tutorial
Approach 2: Making use of the StringBuilder class
The StringBuilder class can also be used into transform a character array to a string. The idea is to cycle through the character array and attach each character to the end of the string because a StringBuilder is a changeable class. Lastly, the string contains the characters' string form.
For instance:
//Converting a Character Array to a String in Java
// Making use of the StringBuilder class
// Importing the necessary classes
importjava.util.*;
// Main class
publicclassGFG {
// Approach 1
// Using the StringBuilder class to convert a character array to a string
publicstaticString toString(char[] ch)
{
// Creating a String class object
StringBuilder SB = newStringBuilder();
// Using the append() function to create a string
for(intj = 0; j<ch.length; j++) {
SB.append(ch[j]);
}
ReturnSB.toString();
}
// Approach 2
// Method of the main driver
publicstaticvoidmain(String args[])
{
// Declaring and initializing input character array
charsh[] = { ‘j’ , ‘a’, ‘v’ , ‘a’ , ‘T’ , ‘p’ , ‘o’ , ‘i’ , ‘n’, ‘t’ , ‘ ’ ,‘t’ , ‘u’ , ‘t’ , ‘o’ , ‘r’ , ‘i’ , ‘a’ , ‘l’};
// To a character array, print the relevant string.
System.out.println(toString(sh));
}
}
Output:
javaTpoint tutorial
Approach 3: Using the String class's function valueOf()
The valueOf() function of the String class can also be used to convert a character array to a string. This method automatically changes the character array to a layout that displays the whole value of the characters in the array. This method converts a string from an int, float, double, char, Boolean, or even an object. We'll accomplish our goal here by transforming our character array into a string.
For instance:
// Converting a Character Array to a String in Java
// Using the String class' function valueOf()
// Importing the necessary classes
importjava.util.*;
// Main class
classJTP {
// Approach 1
// The function valueOf() is used to convert a character array to a string.
publicstaticString toString(char[] ch)
{
// Creating a String class object
String string = String.valueOf(ch);
returnstring;
}
// Approach 2
// Method of the main driver
publicstaticvoidmain(String args[])
{
// The input character array is declared and initialised.
charsh[] ={ ‘j’ , ‘a’, ‘v’ , ‘a’ , ‘T’ , ‘p’ , ‘o’ , ‘i’ , ‘n’, ‘t’ , ‘ ’ ,‘t’ , ‘u’ , ‘t’ , ‘o’ , ‘r’ , ‘i’ , ‘a’ , ‘l’};
// To a character array, print the relevant string.
System.out.println(toString(sh));
}
}
Output:
javaTpoint tutorial
Approach 4: Using the String class's function copyValueOf()
The contents of the character array are copied and then edited without impacting the string to be returned, therefore this approach also allows us to convert the character array to a string, as seen in the following example.
For instance:
//Converting a Character Array to a String in Java
// Using the String class' function copyValueOf()
// String class is being imported.
importjava.util.*;
// Main class
classJTP{
// Method of the main driver
publicstaticvoidmain(String[] args)
{
// The input character array is declared and initialised.
char[] arrr = { ‘j’ , ‘a’, ‘v’ , ‘a’ , ‘T’ , ‘p’ , ‘o’ , ‘i’ , ‘n’, ‘t’ , ‘ ’ ,‘t’ , ‘u’ , ‘t’ , ‘o’ , ‘r’ , ‘i’ , ‘a’ , ‘l’};
// Keeping it in a string
// over a string with copyValueOf()
String strr = String.copyValueOf(arrr);
// To a character array, print the relevant string.
System.out.print(strr);
}
}
Output:
javaTpoint tutorial
Approach 5: In Streams, Using Collectors
With the advent of streams in java8, we immediately use Collectors in streams to alter the members of our character input array, and then use the joining() function to return a single string, which we then print.
For instance:
// Converting a Character Array to a String in Java
// In Streams, Using Collectors, in java8
// Collectos and Stream classes are imported.
// from the package java.util.stream
importjava.util.stream.Collectors;
importjava.util.stream.Stream;
// Main class of this code
classJTP{
// Method of the main driver
publicstaticvoidmain(String[] args)
{
// Character array for custom input
char[] character = { ‘j’ , ‘a’, ‘v’ , ‘a’ , ‘T’ , ‘p’ , ‘o’ , ‘i’ , ‘n’, ‘t’ , ‘ ’ ,‘t’ , ‘u’ , ‘t’ , ‘o’ , ‘r’ , ‘i’ , ‘a’ , ‘l’};
// Collectors are used to collect array elements, and then the joining function is used to return a single string.
String strr = Stream.of(character)
.map(arrr ->newString(arrr))
.collect(Collectors.joining());
// Creating a printout of the Collectors' stream
System.out.println(strr);
}
}
Output:
javaTpoint tutorial