javaapache-commonsapache-stringutils

Apache Commons StringUtils.join creating strange square brackets, commas and whitespacing in output


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:

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?


Solution

  • 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);