java String.join() 与其他字符串连接操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31817290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String.join() vs other string concatenation operations
提问by Ankur Mahajan
I have quickly read over the Java8 String apidocumentation.
我已经快速阅读了 Java8 String api文档。
Now I am little curious about String.join() method to concat/join strings.
现在我对连接/连接字符串的 String.join() 方法有点好奇。
This kind of example has helped me to understand better, though:
不过,这种例子帮助我更好地理解:
//Old way:
String str1 = "John";
String str2 = "Doe";
String result = str1 + " " +str2;
//or by using str1.concat(str2);
//New way:
String result = String.join(" ", str1, str2);
Still, I don't understand which one should I use. Is there any performance or other differences between these two processes.
不过,我不明白我应该使用哪个。这两个过程之间是否有任何性能或其他差异。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by RealSkeptic
String.join
relies on the class StringJoiner
which itself relies on an internal StringBuilder
to build the joined string.
String.join
依赖于StringJoiner
本身依赖于内部StringBuilder
来构建连接字符串的类。
So performance-wise it's much the same as using a StringBuilder
and appending to it, or using a chain of +
(which nowadays are converted to StringBuilder
operations by the compiler).
因此,在性能方面,它与使用 aStringBuilder
并附加到它或使用 a 链+
(如今StringBuilder
已由编译器转换为操作)非常相似。
But the significance of String.join
is not as a general replacement for +
or String.concat
, but in being the "reverse operation" of a String.split
operation. It makes more sense in that context - when you have a bunch of strings that you want to join together using a delimiter - than as a replacement for concat
.
但 的意义String.join
不是作为+
or的一般替代String.concat
,而是作为一个String.split
操作的“逆向操作” 。在这种情况下,它更有意义——当你有一堆字符串想要使用分隔符连接在一起时——而不是作为concat
.
That is, to build an output like "a/b/c/d"
or "(a+b+c+d)"
when you have a
,b
,c
and d
in an array or a list, String.join
or a StringJoiner
would make the operation clear and readable.
也就是说,在数组或列表中构建类似"a/b/c/d"
或"(a+b+c+d)"
当您有a
, b
, c
and的输出d
,String.join
或 aStringJoiner
将使操作清晰易读。
回答by Roberto Attias
str1 + " " + str2
is internally converted into:
str1 + " " + str2
内部转换为:
StringBuffer tmp1 = new StringBuffer();
tmp1.append(str1);
tmp1.append(" ");
String tmp2 = tmp1.toString();
StringBuffer tmp3 = new StringBuffer();
tmp3.append(tmp2);
tmp3.append(str2);
String result = tmp3.toString();
str1.concat(str2)
will not produce the same result, as the space wont be present between the two strings.
str1.concat(str2)
不会产生相同的结果,因为两个字符串之间不会出现空格。
join should be equivalent to
加入应该相当于
StringBuffer tmp1 = new StringBuffer();
tmp1.append(str1);
tmp1.append(" ");
tmp1.append(str2);
String result = tmp.toString();
and hence be faster.
因此会更快。
回答by Rajeev Ranjan
We can use StringJoiner (Java8+)
我们可以使用 StringJoiner (Java8+)
import java.util.StringJoiner;
public class StringJoinerTest {
public static final String FILESEPARATOR = ", ";
public static void main(String[] args) {
StringJoiner sj = new StringJoiner(FILESEPARATOR);
sj.add("Test1");
sj.add("Test2");
sj.add("Test3");
System.out.println(sj);
}
}
Output is
输出是
Test1, Test2, Test3