Scala用逗号分隔数组,每个项目用引号引起来 [英] Scala split Array by comma and each item by quotes

查看:622
本文介绍了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 ,并提供前缀后缀如下:

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(""""""", """","""", """"""")

这篇关于Scala用逗号分隔数组,每个项目用引号引起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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