简体   繁体   English

如何在Java 8中加入不带分隔符的String []?

[英]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);

Javadoc for String.join 用于String.join的Javadoc

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.

相关问题 如何在Java中使用多字符串定界符分割字符串? - How do I split a string by a multiple-string delimiter in Java? 如何加入两个可选 <String> 在Java 8中使用分隔符 - How to join two Optional<String> with a delimiter in Java 8 在Java中使用扫描程序定界符,如何保留用作定界符的字符串? - Using the Scanner delimiter in Java, how do I keep the String that I am using as the delimiter? 如何拆分和连接字符串 Java - How do I split and join string Java 在Java中不带分隔符的情况下拆分字符串,进行数学运算,然后重新组合 - split string in Java without delimiter, do math, then recombine 如何在没有分隔符的情况下拆分单词并对 Java 8 中的拆分字符串执行操作? - How I can split a word without a delimiter and perform operations over the split string in Java 8? 如何在不声明另一个 Scanner 对象的情况下在已扫描的字符串上使用分隔符? - How do I use a delimiter on an already-scanned String without declaring another Scanner object? 如何在Java中以给定的String格式添加定界符? - How do you add a delimiter in a given String format in Java? 如何在Java中使用方括号作为分隔符来解析字符串? - How can I parse string using square brackets as a delimiter in Java? 如何从扫描仪中删除分隔符恢复? (爪哇) - How do I remove delimiter restovers from a scanner? (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM