Solved

Char[] to String

Posted on 2004-10-09
5
220 Views
Last Modified: 2010-03-31
How can I convert Char[] to a String object? I try .toString() but that returns its memory address.
0
Comment
Question by:jtcy
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12266335
String str=new String((byte[])charArray);
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 20 total points
ID: 12266375
char []charArray=new {'H','E','L','L','O'};
String str =new String(charArray);

            ( or)

String str =new String((byte[]charArray);
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12266379
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 12266676
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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12266693
:o\  lol - woops. Thanks MM.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now