Solved

Char[] to String

Posted on 2004-10-09
5
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 61
jsp login check 12 54
Facing this issue for maven proxy setting 2 27
Need Help! Getting a syntax error and don't understand why 3 41
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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