1

I'm getting weird results from Apache Commons Lang's StringUtils.join. Let's say I have:

List<String> values = new LinkedList<>();

values.add("120");
values.add("123456789");
values.add("9000");
values.add("en");

byte[] data = StringUtils.join(values, new char[] {1}).getBytes();

I expected to have 31323001313233343536373839013930303001656e, which is 120.123456789.9000.en, with . as 0x01. But what confuses me is that I'm getting 5b3132302c203132333435363738392c20393030302c20656e5d5b4340333664303437 instead, which converts to [120, 123456789, 9000, en][C@36d047. Is there a gotcha in the way I'm doing this that's causing the weird value?

2
  • 3
    Which join method are you using? I'm not getting any join method which takes 2nd argument as char array. Commented Dec 23, 2013 at 17:41
  • 1
    Great, it was a stupid error, I really thought it had a method to join a char array into a string, but it was calling StringUtils.join(T... x); Commented Dec 23, 2013 at 17:45

1 Answer 1

1

You're using the following method:

public static <T> String join(T... elements)

Joins the elements of the provided array into a single String containing the provided list of elements.

No separator is added to the joined String. Null objects or empty strings within the array are represented by empty strings.

So this method calls toString() on the list of Strings and on the char array, and joins the results.

You want to pass a char or String separator as second argument instead:

StringUtils.join(values, '.').getBytes();
Sign up to request clarification or add additional context in comments.

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.