Scala offers a method called stripMargin that removes the left-hand part of a multiline string up to a specified delimiter (default: "|"). Here is an example:
"""|Foo
|Bar""".stripMargin
returns the string
Foo
Bar
Is there a similar function in Clojure? If not, how would you implement it (most functionally)?
Thanks.
UPDATE: The example I gave is not complete. The stripMargin method also preserves whitespace after the delimiter:
"""|Foo
| Bar""".stripMargin
returns the string
Foo
Bar
There is no such function built in but you write it easily:
user=> (use '[clojure.contrib.string :only (join, split-lines, ltrim)]) //'
nil
user=> (->> "|Foo\n |Bar" split-lines (map ltrim)
(map #(.replaceFirst % "\\|" "")) (join "\n"))
"Foo\nBar"
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