Solved

Char[] to String

Posted on 2004-10-09
5
221 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 39
Java Options in Zimbra Server 2 46
arguments to jar 5 15
Configure a Bean in an XML file 4 6
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

910 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

22 Experts available now in Live!

Get 1:1 Help Now