string Scala:加入一个可迭代的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13529512/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:44:06  来源:igfitidea点击:

Scala: join an iterable of strings

stringscala

提问by scala_newbie

How do I "join" an iterable of strings by another string in Scala?

如何通过 Scala 中的另一个字符串“加入”一个可迭代的字符串?

val thestrings = Array("a","b","c")
val joined = ???
println(joined)

I want this code to output a,b,c(join the elements by ",").

我希望此代码输出a,b,c(通过“,”加入元素)。

回答by Brian Agnew

How about mkString?

mkString怎么

theStrings.mkString(",")

A variant exists in which you can specify a prefix and suffix too.

存在一个变体,您也可以在其中指定前缀和后缀。

See herefor an implementation using foldLeft, which is much more verbose, but perhaps worth looking at for education's sake.

有关使用foldLeft的实现,请参见此处,该实现要冗长得多,但出于教育目的,也许值得一看。