1

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?

4 Answers 4

6

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.)

Sign up to request clarification or add additional context in comments.

5 Comments

IIRC, the last milestone is now due in September (of 2010).
Current M5 milestone seems perfectly usable, too.
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!
This is really sad. So, when people say "java is a high-level language", what do they mean exactly? Is that just wishful thinking?
@Casey When you need to be particularly efficient you can do so in Java without changing language. Best of both worlds. Really happy.
5

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.

1 Comment

Thanks Kevin, fixed it! (And thanks for Collections, I've just started to look closely at them and can't wait to use them!)
2
org.apache.commons.lang.StringUtils.join

7 Comments

OP is specifically asking for an "inbuilt function" [sic]
@Tim Apache commons is de-facto built-in.
He said there should be one, and he's right.... but there isn't, and Apache Commons is the best we have
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..
Apache commons is not de facto built in.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.