Scala按逗号分隔数组,每个项目用引号分隔

时间:2016-07-05 09:24:00

标签: arrays scala

我需要在scala中用逗号分隔数组,并用引号分割每个项目。

scala提供mkString来分割项目,以下示例使用它:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="DynamicFields", Namespace="http://schemas.datacontract.org/2004/07/SomeNamespace")]
[System.SerializableAttribute()]
[KnownType(typeof(ClassA[]))] //This is what I manually added
public partial class DynamicFields : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    ...
}

结果是:

val args = Array("Hello", "world", "it's", "me")
val string = args.mkString(",")

但我需要用引号括起来的每个元素,如下例所示:

Hello,world,it's,me

我可以使用类似下面的地图来实现它

"Hello","world","it's","me"

有没有内置的操作以更礼貌的方式做同样的事情?

谢谢!

2 个答案:

答案 0 :(得分:2)

或者可以使用mkString版本的选项提供前缀后缀,如下所示:

val args = Array("Hello", "world", "it's", "me")
args.mkString(""""""", """","""", """"""")

答案 1 :(得分:0)

您的解决方案看起来很好。较短的版本可能是s"\"$s\"",但它目前不起作用: https://issues.scala-lang.org/browse/SI-6476

相关问题