English 中文(简体)
Java - is there an built in function for concatenating the Strings in a String[]?
原标题:

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?

最佳回答

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

问题回答

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.

org.apache.commons.lang.StringUtils.join

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签