multiline

Dart: How to remove leading spaces in a multiline string (using splitMapJoin) alvin October 7, 2019 - 7:50pm

As far as I know, there’s no built-in way to remove spaces from a multiline string in Dart, but you can write your own function to do this. For example, I just dug into the splitMapJoin method of the Dart String class, and wrote a stripMargin function like this:

String stripMargin(String s) {
    return s.splitMapJoin(
        RegExp(r'^', multiLine: true),
        onMatch: (_) => '\n',
        onNonMatch: (n) => n.trim(),
    );
}
How to left-justify a multiline Scala string alvin April 8, 2019 - 6:01pm

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:

A little Scala `sed` class

A few times during the past year I got tired of trying to remember the Unix/Linux sed syntax while wanting to make edits to many files, so this weekend I wrote a little sed-like Scala class.

A collection of 100+ Scala String examples

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

Problem

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

Solution

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?

Once you know how to do it, this is 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"