How to convert a char array back to a string?

JavaArraysStringChar

Java Problem Overview


I have a char array:

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};

My current solution is to do

String b = new String(a);

But surely there is a better way of doing this?

Java Solutions


Solution 1 - Java

No, that solution is absolutely correct and very minimal.

Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.

Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.

Solution 2 - Java

String text = String.copyValueOf(data);

or

String text = String.valueOf(data);

is arguably better (encapsulates the new String call).

Solution 3 - Java

This will convert char array back to string:

char[] charArray = {'a', 'b', 'c'};
String str = String.valueOf(charArray);

Solution 4 - Java

String str = "wwwwww3333dfevvv";
char[] c = str.toCharArray();

Now to convert character array into String , there are two ways.

Arrays.toString(c);

Returns the string [w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v].

And:

String.valueOf(c)

Returns the string wwwwww3333dfevvv.

In Summary: pay attention to Arrays.toString(c), because you'll get "[w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]" instead of "wwwwww3333dfevvv".

Solution 5 - Java

A String in java is merely an object around an array of chars. Hence a

char[]

is identical to an unboxed String with the same characters. By creating a new String from your array of characters

new String(char[])

you are essentially telling the compiler to autobox a String object around your array of characters.

Solution 6 - Java

Just use String.value of like below;

  private static void h() {

        String helloWorld = "helloWorld";
        System.out.println(helloWorld);

        char [] charArr = helloWorld.toCharArray();

        System.out.println(String.valueOf(charArr));
    }

Solution 7 - Java

package naresh.java;

public class TestDoubleString {

	public static void main(String args[]){
		String str="abbcccddef";	
		char charArray[]=str.toCharArray();
		int len=charArray.length;
		
		for(int i=0;i<len;i++){
			//if i th one and i+1 th character are same then update the charArray
			try{
				if(charArray[i]==charArray[i+1]){
					charArray[i]='0';					
				}}
				catch(Exception e){
					System.out.println("Exception");
				}
		}//finally printing final character string
		for(int k=0;k<charArray.length;k++){
			if(charArray[k]!='0'){
				System.out.println(charArray[k]);
			}		}
	}
}

Solution 8 - Java

 //Given Character Array 
  char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};


    //Converting Character Array to String using String funtion     
    System.out.println(String.valueOf(a));
    //OUTPUT : hello world

Converting any given Array type to String using Java 8 Stream function

String stringValue = 
Arrays.stream(new char[][]{a}).map(String::valueOf).collect(Collectors.joining());

Solution 9 - Java

Try to use java.util.Arrays. This module has a variety of useful methods that could be used related to Arrays.

Arrays.toString(your_array_here[]);

Solution 10 - Java

Try this

Arrays.toString(array)

Solution 11 - Java

String output = new String(charArray);

Where charArray is the character array and output is your character array converted to the string.

Solution 12 - Java

Try this:

CharSequence[] charArray = {"a","b","c"};
	
for (int i = 0; i < charArray.length; i++){
	String str = charArray.toString().join("", charArray[i]);
	System.out.print(str);
}

Solution 13 - Java

You can also use StringBuilder class

String b = new StringBuilder(a).toString();

Use of String or StringBuilder varies with your method requirements.

Solution 14 - Java

1 alternate way is to do:

String b = a + "";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchutsuView Question on Stackoverflow
Solution 1 - JavaA.H.View Answer on Stackoverflow
Solution 2 - JavaDavid TitarencoView Answer on Stackoverflow
Solution 3 - JavaBillzView Answer on Stackoverflow
Solution 4 - JavaAalekhGView Answer on Stackoverflow
Solution 5 - JavaNathan MeyerView Answer on Stackoverflow
Solution 6 - JavaAkash YellappaView Answer on Stackoverflow
Solution 7 - JavaRamnaresh MantriView Answer on Stackoverflow
Solution 8 - JavaArpan SainiView Answer on Stackoverflow
Solution 9 - JavaAdzzView Answer on Stackoverflow
Solution 10 - JavaJessView Answer on Stackoverflow
Solution 11 - JavaAnubhavView Answer on Stackoverflow
Solution 12 - JavaCurtis HView Answer on Stackoverflow
Solution 13 - JavaJagmeet SinghView Answer on Stackoverflow
Solution 14 - JavaPrasanna AView Answer on Stackoverflow