Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert char[] to string in java? [closed]

Tags:

char[] c = string.toCharArray();

but how to convert c back to String type? thank you!

like image 608
codepig Avatar asked Oct 08 '13 02:10

codepig


People also ask

How do you convert this char array to string?

Use the valueOf() method in Java to copy char array to string. You can also use the copyValueOf() method, which represents the character sequence in the array specified. Here, you can specify the part of array to be copied.

How do you convert a character to a string in Java?

We can convert a char to a string object in java by using the Character. toString() method.

What does char [] mean in Java?

The char keyword is a data type that is used to store a single character. A char value must be surrounded by single quotes, like 'A' or 'c'.

How do you get a string from char in Java?

Character is a wrapper class in Java that is used to handle char type objects. The valueOf () method of String class is used to get a string from char. It takes a single argument and returns a string of the specified type. The toString () method of the Character class returns a string of char type value.

What is the use of char character in Java?

Character is a wrapper class in Java that is used to handle char type objects. The valueOf () method of String class is used to get a string from char. It takes a single argument and returns a string of the specified type.

How do I convert an array to a string in Java?

Using valueOf () Method The valueOf () method is a static method of the String class that is also used to 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.

How many bytes is char mychar in Java?

Char is 16 bit or 2 bytes unsigned data type. public class CharToString_toString { public static void main (String [] args) { //input character variable char myChar = 'g'; //Using toString () method //toString method take character parameter and convert string.


2 Answers

You can use String.valueOf(char[]):

String.valueOf(c) 

Under the hood, this calls the String(char[]) constructor. I always prefer factory-esque methods to constructors, but you could have used new String(c) just as easily, as several other answers have suggested.


char[] c = {'x', 'y', 'z'}; String s = String.valueOf(c);  System.out.println(s); 
xyz 
like image 102
arshajii Avatar answered Sep 29 '22 11:09

arshajii


You can use the String constructor:

String(char[] value);

like image 36
Farlan Avatar answered Sep 29 '22 12:09

Farlan