将 Scala 中的列表转换为格式化字符串 [英] Convert list in Scala to a formatted string

查看:47
本文介绍了将 Scala 中的列表转换为格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 List 方法将 Scala 中的 List(1,2,3) 转换为像 "1/2/3" 这样的格式化字符串?

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

推荐答案

看看 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
res1: String = 123

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

这篇关于将 Scala 中的列表转换为格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆