views:

144

answers:

5

Possible Duplicates:
What’s the best way to build a string of delimited items in Java?
Java: convert List<String> to a join()d string

In Java, given a collection, getting the iterator and doing a separate case for the first (or last) element and the rest to get a comma separated string seems quite dull, is there something like str.join in Python?

Extra clarification for avoiding it being closed as duplicate: I'd rather not use external libraries like Apache Commons.

Thanks!

+1  A: 

There is nothing in the standard library, but Guava for example has Joiner that does this.

Joiner joiner = Joiner.on(";").skipNulls();
. . .
return joiner.join("Harry", null, "Ron", "Hermione");
// returns "Harry; Ron; Hermione"

You can always write your own using a StringBuilder, though.

polygenelubricants
I was under the impression that the same Apache library containing StringBuilder had a `join` function no? But its been a bit since I touched that code, so maybe I misremember...
Petriborg
@Petriborg: `java.lang.StringBuilder` is JavaSE, not Apache's. I'm not sure if that answers your question.
polygenelubricants
@Petriborg StringUtil of apache commmons has join (http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html#join%28java.lang.Iterable,%20char%29).
Thomas Jung
A: 

No. There is no such method. Like many others I did my versions of such join for array of strings and collections (iterators).

Michał Niklas
A: 

A compromise solution between not writing extra "utils" code and not using external libraries that I've found is the following two-liner:

/* collection is and object that formats to something like "[1, 2, 3...]"
  (as the case of ArrayList, Set, etc.)
*/
String res = collection.toString();
res = res.substring(1, res.length()-1);
fortran
I would be wary of using this - the `toString` output isn't guaranteed to have any specific format on `java.util.ArrayList`. It's *unlikely* it would change but that would be entirely legal. It would be safer (and arguably clearer) just to write the few lines of "extra utils code", IMHO.
Andrzej Doyle
The API specifies that `AbstractCollection`'s toString is formatted as bracketed comma separated strings: http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/AbstractCollection.html#toString%28%29
fortran
+3  A: 

Nope there is not. Here is my attempt:

/**
 * Join a collection of strings and add commas as delimiters.
 * @require words.size() > 0 && words != null
 */
public static String concatWithCommas(Collection<String> words) {
    StringBuilder wordList = new StringBuilder();
    for (String word : words) {
        wordList.append(word + ",");
    }
    return new String(wordList.deleteCharAt(wordList.length() - 1));
}
BobTurbo
Correct. But it would be better if the method could take a Collection argument instead, so it works regardless of implementation.
Nubsis
ok will change it.
BobTurbo
And from force of habit, I was going to say it should take a `Collection<? extends String>`, but on second thoughts, don't bother. :-)
Andrzej Doyle
@Andrzej yeah, the first time I noticed that String was final I was really disappointed for all the dirty things that I couldn't do xD
fortran
You should add a second param that just takes a character and replace "," with it. I'd say you should provide a default argument but that requires you to write another method D:< One of Java's failings, IMO
Wayne Werner
Yeah I should make it more reusable.. but I can't be bothered now :) I could also do return wordList.substring(0, wordList.length() - 2) or something like that.
BobTurbo
A: 

Not in the standard library. It is in StringUtils of commons lang.

ILMTitan