Consider the following snippet
val myString =
"""
|a=b
|c=d
|""".stripMargin
I want to convert it to a single line with delimiter ;
a=b;c=d;
I tried
myString.replaceAll("\r",";")
and
myString.replaceAll("\n",";")
but it didn't work.
I tried with \n
and it works
scala> val myString = """
| a=b
| c=d
| """.stripMargin
myString: String =
"
a=b
c=d
"
scala> myString.replaceAll("\n",";")
res0: String = ;a=b;c=d;
scala> res0.substring(1, myString.length)
res1: String = a=b;c=d;
I hope it helps
Based on https://stackoverflow.com/a/25652402/5205022
"""
|a=b
|c=d
|""".stripMargin.linesIterator.mkString(";").trim
which outputs
;a=b;c=d
or using single whitespace " "
as separator
"""
|Live long
|and
|prosper!
|""".stripMargin.linesIterator.mkString(" ").trim
which outputs
Live long and prosper!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With