• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Char[] to String

How can I convert Char[] to a String object? I try .toString() but that returns its memory address.
0
jtcy
Asked:
jtcy
  • 2
  • 2
1 Solution
 
sudhakar_koundinyaCommented:
String str=new String((byte[])charArray);
0
 
sudhakar_koundinyaCommented:
char []charArray=new {'H','E','L','L','O'};
String str =new String(charArray);

            ( or)

String str =new String((byte[]charArray);
0
 
DrWarezzCommented:
Use SKs' solution, however, here's the easiest technic to remember (in terms of how it works):

String str = "";
for ( int i =0; i<charArray.length; i++ )
    str += charArray [i];

gL,
[r.D]
0
 
MogalManicCommented:
Don't use DrWarezz's solution.  For string contcationation, Java internally uses a String buffer.  So DrWarezz's solution will compile to approxamatly:
String str = "";
for ( int i =0; i<charArray.length; i++ ) {
    buff=new StringBuffer(str);
    buff.append(charArray [i]);
    str=buff.toString();
}


The String class is actually implemented internally as a character array. So use sudhakar_koundinya's answer.  When you use the
  String str =new String(charArray);
constructor.  The String class will copy your character array into its character array.
0
 
DrWarezzCommented:
:o\  lol - woops. Thanks MM.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now