Aasam Tasaddaq
Aasam Tasaddaq

Reputation: 665

Convert Character array to string in Java

I have a Character array (not char array) and I want to convert it into a string by combining all the Characters in the array.

I have tried the following for a given Character[] a:

String s = new String(a) //given that a is a Character array

But this does not work since a is not a char array. I would appreciate any help.

Upvotes: 15

Views: 44456

Answers (10)

Juvanis
Juvanis

Reputation: 25950

Iterate and concatenate approach:

Character[] chars = {new Character('a'),new Character('b'),new Character('c')};

StringBuilder builder = new StringBuilder();

for (Character c : chars)
    builder.append(c);

System.out.println(builder.toString());

Output:

abc

Upvotes: 7

Johnny Wu
Johnny Wu

Reputation: 1518

Actually, if you have Guava, you can use Chars.toArray() to produce char[] then simply send that result to String.valueOf().

Upvotes: 2

Miguel Durazo
Miguel Durazo

Reputation: 988

Probably an overkill, but on Java 8 you could do this:

Character[] chars = {new Character('a'),new Character('b'),new Character('c')};

String value = Arrays.stream(chars)
                .map(Object::toString)
                .collect( Collectors.joining() );

Upvotes: 3

Harikrishnan
Harikrishnan

Reputation: 1474

int length = cArray.length;
String val="";
for (int i = 0; i < length; i++)
    val += cArray[i];
System.out.println("String:\t"+val);

Upvotes: 0

Tom
Tom

Reputation: 176

how about creating your own method that iterates through the list of Character array then appending each value to your new string.

Something like this.

public String convertToString(Character[] s) {
   String value;

   if (s == null) {
     return null;
   }

   Int length = s.length();
   for (int i = 0; i < length; i++) {
     value += s[i];
   }

   return value;
} 

Upvotes: 2

Stephen C
Stephen C

Reputation: 718798

The most efficient way to do it is most likely this:

Character[] chars = ...

StringBuilder sb = new StringBuilder(chars.length);
for (Character c : chars)
    sb.append(c.charValue());

String str = sb.toString();

Notes:

  1. Using a StringBuilder avoids creating multiple intermediate strings.
  2. Providing the initial size avoids reallocations.
  3. Using charValue() avoids calling Character.toString() ...

However, I'd probably go with @Torious's elegant answer unless performance was a significant issue.


Incidentally, the JLS says that the compiler is permitted to optimize String concatenation expressions using equivalent StringBuilder code ... but it does not sanction that optimization across multiple statements. Therefore something like this:

    String s = ""
    for (Character c : chars) {
        s += c;
    }

is likely to do lots of separate concatenations, creating (and discarding) lots of intermediate strings.

Upvotes: 9

rees
rees

Reputation: 1576

It's probably slow, but for kicks here is an ugly one-liner that is different than the other approaches -

Arrays.toString(characterArray).replaceAll(", ", "").substring(1, characterArray.length + 1);

Upvotes: 3

Yogendra Singh
Yogendra Singh

Reputation: 34367

First convert the Character[] to char[], and use String.valueOf(char[]) to get the String as below:

    char[] a1 = new char[a.length];
    for(int i=0; i<a.length; i++) {
        a1[i] = a[i].charValue();
    }
    String text = String.valueOf(a1);
    System.out.println(text);

Upvotes: 3

Kiith Nabaal
Kiith Nabaal

Reputation: 466

At each index, call the toString method, and concatenate the result to your String s.

Upvotes: 2

Torious
Torious

Reputation: 3424

Character[] a = ...
new String(ArrayUtils.toPrimitive(a));

ArrayUtils is part of Apache Commons Lang.

Upvotes: 18

Related Questions