multi-line strings with scala

2010-09-14 comments | programming, scala

I love the multi-line strings from python. very usefull, but there is always the problem with the indentation. python has an elegant solution, the textwrap.dedent(text) function.

scala has a almost as elegant solution, the stripMargin function.

see it here in action:

"""foo
   bar"""
foo
   bar

"""|foo
   |bar""".stripMargin
foo
bar

other than pythons function, scalas stripMargin function need a special character to which leading whitespaces are removed. default is the pipe |, but you can use any charater you want:

"""#spam
   #eggs""".stripMargin('#')
spam
eggs

hth

blog comments powered by Disqus