A collection of 100+ Scala String examples alvin August 22, 2017 - 5:43am

This page contains a collection of over 100 Scala String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)

First, here are some basic uses of the Scala String class to help get us warmed up:

How to create multiline strings in Scala alvin July 9, 2017 - 6:05pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.2, “How to Create Multiline Strings in Scala.”


You want to create multiline strings within your Scala source code, like you can with the “heredoc” syntax of other languages.


In Scala you create multiline strings by surrounding your text with three double quotes:

How to convert a multiline Scala multiline String to an Array (or Seq)

Scala string FAQ: How do I convert a multiline String to an Array or Seq or List in Scala?

I was just working on a program to parse Apache access log records, and ran into this situation. Here’s the solution.

Given a multiline string, like this:

Scala XML examples: XML literals, mixing XML and Scala source code, XPath searching

A really terrific feature about Scala is that XML handling is built into the language. This means you don't have to deal with XML as String objects, you deal with it as XML objects.

Here are just a few examples of using XML in Scala. First, you can create an XML literal like this:

scala> val hello = <p>Hello, world</p>
hello: scala.xml.Elem = <p>Hello, world</p>

Again, note that this is not a String, there are no double quotes; we've just defined an XML literal in Scala.

How to enter multiline commands (statements) into the Scala REPL

When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.

As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up:

AppleScript text dialog - How to display multiline text in a dialog

AppleScript dialog text FAQ: "How can I display an AppleScript dialog showing multiple lines of text?"

This is actually surprisingly easy, and there are a couple of ways to do it. Here are a few examples:

display dialog "Line 1
Line 2
Line 3"

Running this program displays the following AppleScript text dialog:

An AppleScript dialog that displays several lines of text.

Here's another way to accomplish the same thing:

LaTeX multiline comments

I often have a need to create LaTeX comments that span multiple lines. Of course you can create single line comments in LaTeX using the percent character like this:

% this is a comment

But I want to be able to create LaTeX comments that go on for multiple lines. Fortunately, if you know that you're supposed to include the verbatim package, this is pretty easy.

LaTeX multiline comments example

The first step is to include the verbatim package, like this:

Use Perl here documents to print multiple lines of output

Summary: How to use Perl here documents, i.e., the Perl heredoc syntax.

Perl offers a convenient way of printing multiple lines of output through an interesting feature known as a "Perl here document". A here document, or heredoc, lets you easily store multiple lines of text in a Perl variable.

A multiline Perl here document works like this: