2

Now I am developing a simple project on Java.In that I include String Builder to append and print strings on console.When I am trying to append string array into string Builder,it prints object instead of array of strings on console.any one please tell whether my try is right or wrong.

2
  • 1
    post your code with your story Commented Oct 7, 2014 at 11:22
  • what is your expected output. Commented Oct 7, 2014 at 11:24

3 Answers 3

6
  • Add Arrays.toString(yourArray) if you want to add the representation of your String array.
  • If it's a multiple-dimension array (e.g. a String[][]), then add Arrays.deepToString(yourArray) to your StringBuilder.
  • Adding the array itself will add the default String representation.
  • Finally, if you need control over how you represent your array to String, you'll need to iterate the elements yourself and add them to the StringBuilder in a customized format.

Edit

As a side-note, to make this work with arrays of custom Objects, you'll probably want to override the Object.toString method, otherwise your elements will be printed with the default String representation of Object (i.e. this).

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

6 Comments

@Nithya you're welcome! Also see my edit on custom Objects.
is it possible to ignore the '[' and ']' during this Arrays.toString()? Because, am trying to append the elements from String[] to StringBuilder that has some elements already and I do not want to include a [ and ] in between.
@AshokMA you can just remove the 1st and last character, that will consistently remove the square brackets in a one-dimensional array. For multi-dimensional arrays, you probably need to keep them, lest you want to flatten it (in which case, replace any occurrence of the square brackets with empty strings).
@Mena yes I can do that. But, I am afraid that it will make my code ugly :( However, I found Apache Commons Lang StringUtils#join() very useful in this case. :)
@AshokMA arguably a little and safe string manipulation should be better than adding a dependency to your project, but it really depends on the context.
|
4

Array is an Object. Do not directly append the array. Just iterate on array and append each element of array to your String builder.

for(String str : arr) {
    builder.append(str);
}

Comments

0

/** * <p>Joins the elements of the provided array into a single String * containing the provided list of elements.</p> * * <p>No delimiter is added before or after the list. * A <code>null</code> separator is the same as an empty String (""). * Null objects or empty strings within the array are represented by * empty strings.</p> * * <pre> * StringUtils.join(null, *) = null * StringUtils.join([], *) = "" * StringUtils.join([null], *) = "" * StringUtils.join(["a", "b", "c"], "--") = "a--b--c" * StringUtils.join(["a", "b", "c"], null) = "abc" * StringUtils.join(["a", "b", "c"], "") = "abc" * StringUtils.join([null, "", "a"], ',') = ",,a" * </pre> * * @param array the array of values to join together, may be null * @param separator the separator character to use, null treated as "" * @param startIndex the first index to start joining from. It is * an error to pass in an end index past the end of the array * @param endIndex the index to stop joining from (exclusive). It is * an error to pass in an end index past the end of the array * @return the joined String, <code>null</code> if null array input */

Apache Commons Lang StringUtils#join(). is so cool. Hope this helps.

Comments

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.