views:

254

answers:

1

Hi, I am fairly new to scala and I have the need to convert a string that is pipe delimited to one that is comma delimited, with the values wrapped in quotes and any quotes escaped by "\"

in c# i would probably do this like this

string st = "\"" + oldStr.Replace("\"", "\\\\\"").Replace("|", "\",\"") + "\""

I haven't validated that actually works but that is the basic idea behind what I am trying to do. Is there a way to do this easily in scala?

+5  A: 

Similarly:

val st = "\"" + oldStr.replaceAll("\"", "\\\\\"").replaceAll("\\|", "\",\"") + "\""

Could also be:

val st = oldStr.replaceAll("\"","\\\\\"").split("\\|").mkString("\"","\",\"","\"")
Mitch Blevins