O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required