Scala用逗号分隔数组,每个项目用引号引起来
问题描述:
我需要在scala中用逗号分割数组,并用引号将每个项目分割。
I need to split an array by comma in scala and each item by quotes.
scala提供mkString来分割项目,下面的示例使用它:
scala offers mkString to split items, the following example uses it:
val args = Array("Hello", "world", "it's", "me")
val string = args.mkString(",")
结果为:
Hello,world,it's,me
但我需要用引号将每个元素括起来,如以下示例所示:
but I need each element enclosed by quotes as in the following example:
"Hello","world","it's","me"
我可以使用以下地图来实现它
I can implement it using a map like the following one
args.map(entry => s""""${entry}"""" ).mkString(",")
是否有任何内置操作以更礼貌的方式执行相同操作?
is there any builtin operation that does the same in a more polite way ?
谢谢!
答
或者可以使用 mkString $ c的版本$ c>,并提供前缀和后缀ng>如下:
Or maybe use the version of mkString
with option to provide prefix and suffix as below:
val args = Array("Hello", "world", "it's", "me")
args.mkString(""""""", """","""", """"""")