[英]How do I join a String[] without a delimiter in Java 8?
There are lots of questions about how to join a String[]
in Java 8 with a delimiter, but how should a String[]
be joined without a delimiter? 关于如何在Java 8中使用分隔符将
String[]
连接起来,存在很多问题 ,但是如何在不使用分隔符的情况下连接String[]
?
For example, {"a", "b", "c"}
becomes "abc"
. 例如,
{"a", "b", "c"}
变为"abc"
。
Note: I know how to write a small function for this myself, so please do not leave custom solutions. 注意:我自己知道如何为此编写一个小函数,因此请不要留下自定义解决方案。 I am looking for a one-liner from the standard library.
我正在从标准库中寻找一个班轮。
public class Test {
public static void main(String[] args) {
System.out.println( String.join("", new String[]{"a", "b", "c"}) );
}}
Outputs: abc 输出:abc
The relevant part being String.join("", arrayStrings);
相关的部分是
String.join("", arrayStrings);
Adding to what has already been said, you can even do it as simple as this if you so prefer: 除了已经说过的内容,如果您愿意,您甚至可以像这样简单:
public class Test {
public static void main(String[] args) {
System.out.println(String.join("", "a", "b", "c"));
}
}
Ie you don't even have to wrap it in an array; 也就是说,您甚至不必将其包装在数组中。 since
String.join
takes a ...
(variable argument), you can just specify any number of String
arguments without explicitly wrapping them in an String[]
object. 由于
String.join
接受...
(可变参数),因此您可以指定任意数量的String
参数,而无需将它们显式包装在String[]
对象中。
Note : the big disadvantage of the approach above is that it obscures the distinction between the delimiter and the delimited strings; 注意 :上面方法的最大缺点是,它模糊了定界符和定界字符串之间的区别; @Aaron's answer is generally preferable.
通常最好使用@Aaron的答案。 But anyhow, it deserves to be mentioned that you can actually write it like this also.
但是无论如何,值得一提的是您实际上也可以这样编写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.