string

A collection of 100+ Scala String examples alvin August 7, 2017 - 11:35am
Table of Contents1 - String equality2 - Multiline strings3 - String interpolation/substitution4 - Substrings5 - Treating a String as an Array[Char]6 - Using map and for7 - Regular expressions8 - Transforming arrays to a String9 - A lot of String method 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:

A ‘printf’ format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

Android: How to convert a list of strings or objects to a single string alvin August 2, 2017 - 4:39pm

Android FAQ: How do I convert a list of strings (or a list of objects) to a single, combined string?

In Android, if you want to convert a list of strings to a String, I just saw this approach:

How to write “If string starts with” in Drupal 8 Twig templates

If you ever need to write a “string starts with” comparison in Drupal 8 Twig templates, I just used this approach in a node.html.twig template file and I can confirm that it works:

{% if uri starts with '/foo' %}

More accurately, what I did was to first get the URI for the current Drupal node, and then I perform that test:

Scala: How to parse a number from a String alvin July 10, 2017 - 1:25pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 2.1, “Parsing a Number from a String.”

Problem

You want to convert a String to one of Scala’s numeric types (Byte, Double, Int, Float, Long, Short).

Scala: Finding the difference, intersection, and distinct characters in a String

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 1.11, “String Differences, Intersections, and Distinct Characters.”

Problem

In Scala, you need to perform advanced string operations, such as finding the difference between two strings, the common characters between two strings, or the unique characters in a string.

How to find regex patterns in Scala strings

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.7, “Finding Patterns in Scala Strings.”

Problem

You need to determine whether a Scala String contains a regular expression pattern.

How to split strings in Scala

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

Problem

You want to split a string into parts based on a field separator, such as a string you get from a CSV or pipe-delimited file.

Solution

Use one of the split methods that are available on String objects:

How to create multiline strings in Scala

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: