views:

307

answers:

4

Or a better way than this?

String concat(String[] strings) {
  StringBuilder out = new StringBuilder();

  for(String next: strings) {
    out.append(next);
  }

  return out.toString();
}

No worries if no, I just feel like there should be a built in?

+3  A: 
org.apache.commons.lang.StringUtils.join
HappyCoder
OP is specifically asking for an "inbuilt function" [sic]
Tim
@TimApache commons is de-facto built-in.
HappyCoder
He said there should be one, and he's right.... but there isn't, and Apache Commons is the best we have
skaffman
I'm all for the Apache solution, and as such it's a valuable contribution, but it's not what was asked.. it might be overkill to include a dependency for four lines of code..
Tim
Apache commons is **not** de facto built in.
cletus
@Tim: has asked for a better way, and this is the better way.
skaffman
@skaffman I agree that this is the better way. I include this library in every project I work on, it just makes things easier.
Ravi Wallau
+5  A: 

No, not in the current Java library.

In JDK7 you should be able to write String.join("", strings). It was found that "85%" of the uses for wanting an index in the posh for loop was to do a string join (which you can do without anyway).

I guess if you want to be uber efficient, you could write it as something like:

public static String concat(String... strs) {
    int size = 0;
    for (String str : strs) {
        size += str.length;
    }

    final char[] cs = new char[size];
    int off = 0;
    try {
        for (String str : strs) {
            int len = str.length();
            str.getChars(0, len, cs, off);
            off += len;
        }
    } catch (ArrayIndexOutOfBoundsException exc) {
        throw new ConcurrentModificationException(exc);
    }
    if (off != cs.length) {
        throw new ConcurrentModificationException();
    }
    return new String(cs);
}

(Not compiled or tested, of course.)

Tom Hawtin - tackline
Finally! When does JDK7 come? =)
BalusC
In a year, lol :)
HappyCoder
IIRC, the last milestone is now due in September (of 2010).
Tom Hawtin - tackline
Current M5 milestone seems perfectly usable, too.
skaffman
Fantastic, thanks for the info! My use case doesn't warrant the extra confusion that your method would cause my co-workers but thanks for being so thorough. Roll on Java 7!
gav
+4  A: 

Take a look at the new Google Guava libraries, which will incorporate Google Collections once it passes from 1.0RC4 to 1.0. Guava and Collections give you quite a bit of power and elegance, and are already used extensively in Google production code.

The Joiner class suits your example perfectly:

String[] strings = { "Stack", "Overflow", ".com" };
String site = Joiner.on("").join(strings);

Aleksander Stensby has a nice four part exploration of Guava/Collections.

Like Apache Collections, it's not part of the JDK, though it builds very carefully on top of java.util.collection.

Jim Ferrans
small correction: Joiner.on("").join(strings).
Kevin Bourrillion
Thanks Kevin, fixed it! (And *thanks* for Collections, I've just started to look closely at them and can't wait to use them!)
Jim Ferrans
A: 

Second recommendation to look at Google Guava.

The Google Collections was released last week, and this week, Guava has been released for testing. The Google Collections stuff is solid and the API will not change. I much prefer Google Collections over the apache one, specifically because its fully generic. The Google folks also claim that its fast enough for them to use in production, which is fairly impressive, altho I can't verify that personally.

fishtoprecords