Java 11 here. I am trying to use org.apache.commons.lang3.StringUtils.join
to take a list of strings and join/concatenate them into a single string, separated by an @
sign. If there is only one (1) string in the list, then the joins/concatenated output should be that same string, no delimiter. Hence:
[ "Hello", "World" ]
, output should be "Hello@World"
[ "Hello" ]
, output should be "Hello"
My best attempt thus far is:
String output = StringUtils.join("@", inputList);
But for an inputList
of [ "Hello", "World" ]
this gives me output of:
@[Hello, World]
And for an inputList
of [ "Hello" ]
, this gives me output of:
@[Hello]
Can anyone spot where I'm going awry?
The method itself works, you just need to put in the array first, then the delimiter. In the documentation there is an example, and when I just tried it it worked.
The following code outputs Hello@World
, I just switched the parameters.
String[] inputList = new String[] { "Hello", "World" };
String joined = StringUtils.join(inputList, "@");
System.out.println(joined);