Strings and Multiline Raw Strings

String in Scala is simply java.lang.String. You can use String just like you do in Java. However, Scala does provide a few additional conveniences when working with String.

Scala can automatically convert a String to scala.runtime.RichString. This brings a few useful methods like capitalize, lines, and reverse to String.

It’s really simple in Scala to create a string that runs multiple lines—no more of those messy +=. Place the multiple lines of strings within three double quotes ("""…"""). That’s Scala’s support for the so-called here documents, or heredocs. To see it in action, let’s create a string that runs a few lines long:

FromJavaToScala/MultiLine.scala
 
val​ str = ​"""In his famous inaugural speech, ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.