Scala String Interpolation In Depth With Examples

Before reading this post, please go through my previous Scala Language basic concepts. In this post, we are going to discuss about Scala’s String Interpolation concept with suitable examples. And also we will discuss similar kind of concept supported by Java Language.

Post Brief Table of Content:

  • Introduction
  • What is String Interpolation?
  • String Interpolation Rules
  • Types of String Interpolations In Scala
  • s String Interpolator
  • f String Interpolator
  • raw String Interpolator
  • Compare Scala’s String Interpolation With Java

Introduction

Scala Team has introduced String Interpolation concept in Scala 2.10 version to facilitate String processing in better way. As we know, Scala treats String a sequence of Characters.

This String Interpolation allows defining the following things in a String Literal to process them by the Scala Compiler:

  • Variables
  • Expressions
  • Method or Function Calls
  • Object Fields

We will discuss each these concepts one by one in coming sections.

What is String Interpolation?

In Scala, String Interpolation means that the replacement of defined variables or expressions in a given String with values.

String Interpolation is used to process String literals in easy way. To use this concept, We should follow some rules and syntax.

String Interpolation Rules:

In Scala, we should follow these rules to use String Interpolation concept to process String Literals. These rules are same for all kinds of String Interpolation.

  • Define Strings starting with s or f or raw letter
  • Define variables in that String with $variable_name syntax
  • Define expressions in that String with ${expression} syntax
  • Define Object fields with ${object.field} syntax

Types of String Interpolations In Scala

Scala supports mainly three kinds of String Interpolation:

  • s String Interpolator
  • f String Interpolator
  • raw String Interpolator

Now we will take each kind of String Interpolator and discuss it with some examples in coming sections.

s String Interpolator

In this String Interpolation, we should define String Literals with s String Interpolator. That means String Literal should start with ‘s’ letter.

We use this Interpolation concept to access Variables, Object Fields, Function calls etc.

Example-1:- How to access Variables in a String Literal


scala>val title1 = "Play"
title1: String = Play
scala>val title2 = "Scala"
title2: String = Scala
scala>val book1 = "Book($title1 2 for $title2)"
book1: String = Book($title1 2 for $title2)
scala>val book2 = s"Book($title1 2 for $title2)"
book2: String = Book(Play 2 for Scala)

Example-2:- How to access Object Fields in a String Literal


scala> class Person(val firstName: String, val lastName:String)
defined class Person
scala> val person1 = new Person("Ram","Posa")
person1: Person = [email protected]
scala> val str1 = s"Person fullname: ${person1.firstName}-${person1.lastName}"
str1: String = Person fullname: Ram-Posa

Example-3:- How to access Function or Method Calls in a String Literal


scala> class Person(val firstName: String, val lastName:String) {
     |   def fullName = firstName + "-" + lastName
     | }
defined class Person
scala> val person1 = new Person("Ram","Posa")
person1: Person = [email protected]
scala> val str2 = s"Person fullname: ${person1.fullName}"
str2: String = Person fullname: Ram-Posa

Example-4:- How to access Expressions in a String Literal


scala> val add10and20 = s"Addition of 10 and 20 = ${10 + 20}"
add10and20: String = Addition of 10 and 20 = 30

f String Interpolator

In this String Interpolation, we should define String Literals with f String Interpolator. That means String Literal should start with ‘f’ letter.

We use this Interpolation concept to format numbers in easy way.

Example-1:- How to format Numbers in a String Literal


scala> val itemPrice = 10.5
itemPrice: Double = 10.5
scala> val str = s"Item Price : $itemPrice"
str: String = Item Price : 10.5
scala> val str = s"Item Price : $itemPrice%.2f"
str: String = Item Price : 10.5%.2f
scala> val str = f"Item Price : $itemPrice%.2f"
str: String = Item Price : 10.50

Here we can observe that if we use s String Interpolator to format numbers, we don’t get expected result. In this use cases, we should use f String Interpolator only.

raw String Interpolator

In this String Interpolation, we should define String Literals with raw String Interpolator. That means String Literal should start with ‘raw’.

It is used to accept escape sequence characters like ‘n’ as it is in a String literal. That means when we don’t want to process or evaluate escape sequence characters then we should use raw String Interpolator.

Example-1:- How s String Interpolator processes Escape Sequence Characters in a String Literal


scala> val itemName = "Laptop"
itemName: String = Laptop
scala> val itemPrice = 499.99
itemPrice: Double = 499.99
scala> val str = s"Item Details: n Name: $itemName t Price : $itemPrice%.2f"
str: String =
Item Details:
 Name: Laptop    Price : 499.99%.2f

Example-2:- How raw String Interpolator works on Escape Sequence Characters in a String Literal


scala> val itemName = "Laptop"
itemName: String = Laptop
scala> val itemPrice = 499.99
itemPrice: Double = 499.99
scala> val str = raw"Item Details: n Name: $itemName t Price : $itemPrice%.2f"
str: String = Item Details: n Name: Laptop t Price : 499.99%.2f

Compare Scala’s String Interpolation With Java

When we compare Scala’s String Interpolation With Java, Java does not support this concept. However, to to this kind of job for Strings, Java has a ‘printf’ function.

In Java, ‘printf’ function takes two kinds of parameters: first parameter is actual processed String and rest all are substituted variables or values.

Example:-


String playerName = "Ram";
float score = 95.5;
System.out.printf("Player Details: %s %d.2f", playerName, score);

Output:-


Player Details: Ram 95.50

Scala’s f String Interpolator is similar to Java’s printf function.

That’s it all about “Scala String Interpolation”. We will discuss some more Scala Programming concepts in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions.

By admin

Leave a Reply

%d bloggers like this: