将 Scala 中的列表转换为格式化字符串

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

Convert list in Scala to a formatted string

scala

提问by Pooya

How can I convert the List(1,2,3)in Scala to a formatted string like "1/2/3"with the List methods?

如何将List(1,2,3)Scala 中的转换为类似于"1/2/3"List 方法的格式化字符串?

回答by Jatin

Have a look at mkString. In short:

看看mkString。简而言之:

Displays all elements of this list in a string using a separator string (In your case "/")

使用分隔符字符串在字符串中显示此列表的所有元素(在您的情况下为“/”)

scala> List(1,2,3).mkString("/")
res0: String = 1/2/3

scala> List(1,2,3).mkString
res0: String = 123

// def mkString(start: String,sep: String,end: String): String 
scala> List(1,2,3).mkString("@", "/", "@")
res1: String = @1/2/3@