2

What is the best way to convert a Character[] to char[] or a String ?

One way is to manually loop through the Character array presently in a for(char c : CharacterArray) loop , appending to a StringBuilder. StringBuilder seems to accept a Character[] directly also , but is that all to it ?

6
  • @jleedev : Think of me as a type-safety nut :-) Commented Jun 28, 2011 at 21:01
  • 1
    @Bhaskar, there is almost never a good reason to use a Character[] The best thing to do is to refactor your code so you don't have one in the first place. Use a char[] or a StringBuilder instead. Commented Jun 28, 2011 at 21:11
  • @Peter Lawrey: I have a generic type in which one generic type param has to be 'char' - I dont know any other way to handle this other than using a Character , which then lands me in the situation mentioned. Commented Jun 28, 2011 at 21:17
  • @Bhaskar, Instead of List<Character> you should use StringBuilder. What type of generic collection are you using? Commented Jun 28, 2011 at 21:20
  • @Peter Lawrey : Its a Map<Character,Integer>. I am analyzing text/words and storing the letters-freq map, to be processed further by other code. Commented Jun 28, 2011 at 21:23

2 Answers 2

3

commons-lang has a toPrimitive method, but there's nothing like this in the standard API. The only way is to loop through the array and transform each Character into a char (and decide what to do with null values).

Sign up to request clarification or add additional context in comments.

Comments

2

I would recommend converting the Character[] to a char[] using a simple loop and then passing it into new String();

    Character[] characters = new Character[128];
    char[] chars = new char[characters.length];
    int length = characters.length;
    for(int i=0; i<length; i++) {
        chars[i] = characters[i].charValue();
    }
    String string = new String(chars);

Another alternative would be to create a StringBuilder and loop through and add each character individually.

    Character[] characters = new Character[128];
    StringBuilder sb = new StringBuilder(characters.length);
    for(Character c : characters) {
        sb.append(c);
    }
    String string = sb.toString();

I have a feeling the first approach might be more efficient, but I'm not sure.

5 Comments

If you initialize the StringBuilder with enough capacity, the second way should be efficient.
True, the first one is much faster. The StringBuilder will each time recreate a new buffer of one character bigger than the previous one. I checked the Java source code of StringBuilder. Except when you construct the StringBuilder with the given size of the array. Then, there is no need for recreating the buffer. Then the performance will be the same.
actually if you do new StringBuilder(characters.length); it will be the most efficient if the appends and unboxing are inlined by JIT as the underlying char[] in the builder will be used in the resulting String
Foreach is less efficient than the standard for loop. Also String uses StringBuilder as its implementation so String+= does not have a negative performance.
@All I updated to add the length to constructor of StringBuilder. Thanks for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.