添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Get full access to Scala Cookbook and 60K+ other titles, with a free 10-day trial of O'Reilly.

There are also live events, courses curated by job role, and more.

Start your free trial

Although this works, the second and third lines in this example will end up with whitespace at the beginning of their lines. If you print the string, it looks like this:

This is
    a multiline
    String

You can solve this problem in several different ways. First, you can left-justify every line after the first line of your string:

val foo = """This is
a multiline
String"""

A cleaner approach is to add the stripMargin method to the end of your multiline string and begin all lines after the first line with the pipe symbol ( | ):

val speech = """Four score and
               |seven years ago""".stripMargin

If you don’t like using the | symbol, you can use any character you like with the stripMargin method:

val speech = """Four score and
               #seven years ago""".stripMargin('#')

All of these approaches yield the same result, a multiline string with each line of the string left justified:

Four score and
seven years ago

This results in a true multiline string, with a hidden \n character after the word “and” in the first line. To convert this multiline string into one continuous line you can add a replaceAll method after the stripMargin call, replacing all newline characters with blank spaces:

val speech = """Four score and
               |seven years ago
               |our fathers""".stripMargin.replaceAll("\n", " ")

This yields:

Four score and seven years ago our fathers

Another nice feature of Scala’s multiline string syntax is that you can include single- and double-quotes without having to escape them:

val s = """This is known as a
        |"multiline" string
        |or 'heredoc' syntax.""". stripMargin.replaceAll("\n", " ")

This results in this string:

This is known as a "multiline" string or 'heredoc' syntax.

Get Scala Cookbook 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.

Start your free trial

© 2023, O’Reilly Media, Inc. All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.

Terms of service Privacy policy Editorial independence

Don’t leave empty-handed

Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.

It’s yours, free.

Get it now

Check it out now on O’Reilly

Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.

Start your free trial Become a member now