Tutorials about the Scala programming language.

Scala: Generating a sequence/list of all ASCII printable characters

I ran into a couple of interesting things today when trying to generate random alphanumeric strings in Scala, which can be summarized like this. I won’t get into the “random” stuff I was working on, but here are a couple of examples of how to generate lists of alphanumeric/ASCII characters in Scala:

scala> val chars = ('a' to 'Z').toList
chars: List[Char] = List()

scala> val chars = ('A' to 'z').toList
chars: List[Char] = 
List(A, B, C, D, E, F, G, H, I, J, K, L, 
     M, N, O, P, Q, R, S, T, U, V, W, X, 
     Y, Z, [, \, ], ^, _, `, a, b, c, d, 
     e, f, g, h, i, j, k, l, m, n, o, p, 
     q, r, s, t, u, v, w, x, y, z)

scala> val chars = (' ' to 'z').toList
chars: List[Char] = 
List( , !, ", #, $, %, &, ', (, ), *, +, 
     ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 
     8, 9, :, ;, <, =, >, ?, @, A, B, C, 
     D, E, F, G, H, I, J, K, L, M, N, O, 
     P, Q, R, S, T, U, V, W, X, Y, Z, [, 
     \, ], ^, _, `, a, b, c, d, e, f, g, 
     h, i, j, k, l, m, n, o, p, q, r, s, 
     t, u, v, w, x, y, z)

Implicit methods/functions in Scala 2 and 3 (Dotty extension methods)

Table of Contents1 - Scala 2: Create the method in an implicit class2 - Scala 3 (Dotty): Adding methods to closed classes with extension methods

Scala lets you add new methods to existing classes that you don’t have the source code for, i.e., classes like String, Int, etc. For instance, you can add a method named hello to the String class so you can write code like this:


which yields output like this:

"Hello, Joe"

Admittedly that’s not the most exciting method in the world, but it demonstrates the end result: You can add methods to a closed class like String. Properly (tastefully) used, you can create some really nice APIs.

In this article I’ll show how you can create implicit methods (also known as extension methods) in Scala 2 and Scala 3 (Dotty).

A little Scala project to convert AsciiDoc to clean, simple HTML

Table of Contents1 - A shell script solution2 - A JavaFX GUI3 - Summary

I recently started using AsciiDoc to write a new book. A great thing about it is that unlike Markdown, you can use AsciiDoc to write a book and get all of the features you want in a book, including linking between anything, captions for tables and figures, indexes, etc. Because this got me started using AsciiDoc I thought, “Wouldn’t it be nice if I could also use AsciiDoc to write blog posts like this one?”

Sadly, I quickly ran into a problem: I couldn’t find a good way to convert AsciiDoc into HTML, or even Markdown. There are tools to convert AsciiDoc to HTML, but for some reason they take the approach of including a ton of markup in the HTML (divs, spans, and attributes), and as far as I can tell there’s no way to turn off that markup.

Back to top

A shell script solution

Scala: Convert a String with newline characters to a sequence/list of strings

If you ever need a Scala method/function to convert a string with newline characters in it to a sequence of strings (Seq[String]), here you go:

def convertStringWithNewlinesToSeq(s: String): Seq[String] =

You can convert the final result to a Vector, Seq, List, ArrayBuffer, Array, etc., but I prefer Vector. The Scala REPL demonstrates how it works:

scala> convertStringWithNewlinesToSeq("")
res0: Seq[String] = Vector("")

scala> convertStringWithNewlinesToSeq("foo")
res1: Seq[String] = Vector(foo)

scala> convertStringWithNewlinesToSeq("foo\nbar\nbaz")
res2: Seq[String] = Vector(foo, bar, baz)

scala> convertStringWithNewlinesToSeq("foo\nbar\nbaz\n\n")
res3: Seq[String] = Vector(foo, bar, baz)

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: