By Alvin Alexander. Last updated: June 26, 2022
If you ever need to left-justify a multiline Scala string, the solution is to use the stripMargin
method, as shown in this example:
def getWordFormatted(s: String): String = { s""" | |Word of the Day |--------------- |Word: $s """.stripMargin }
The | character has a special meaning when used in conjunction with stripMargin
, so that method results in a string like this being output:
Word of the Day --------------- Word: the word
For more information on how to use stripMargin
, see How to create multiline strings in Scala and 100 Scala string examples.