How to Convert a Char Array Back to a String

How to convert a char array back to a string?

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.

How to convert char array back to string

String str = String.valueOf( chars );

or

String str = new String( chars );

Convert a Char Array to a String

Use join:

string = s.join("");

Some problems with converting char array to string

    var name = "ромка"
var charName = name.toLowerCase().toCharArray()
charName[0] = charName[0].toUpperCase()

name = String(charName)

Java convert individual characters from char array to String array

No String(char) constructor is defined in String. So you cannot do :

String word1[][]  = ...;
word1[i][0] = new String(charArray[i]); //This is where problem occurs

What you need is String.valueOf(char c) :

word1[i][0] = String.valueOf(charArray[i]); 

How to convert character array (char[]) to String array (String[]) without looping through char array and by using predefined methods in java?

char[] charArray={'A','B','C'}; // Character array initialized
/**
*Below line will first convert a charArray to string using
*String(char[]) constructor and using String class method
*split(regularExpression) the converted string will
*then be splited with empty string literal delimiter which in turn
*returns String[]
**/
String[] result=new String(charArray).split("");

Convert Char array to String array

simply change

currentGuessPhrase[x] = currentGuessArray[x].toString();

to

currentGuessPhrase[x] = new String(currentGuessArray[x]);


Related Topics



Leave a reply



Submit