Pregunta

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?

¿Fue útil?

Solución

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top