Scala String Interpolation in detail

245 total views, 3 views today

Welcome to Scala series. In this tutorial, you will learn about Scala String Interpolation in detail.

Scala String interpolation

Scala String interpolation brings us a different way to create a string. It allows us to create dynamic strings with dynamic values during runtime.

Let us see a few examples

“s” Interpolation

Let us print the age dynamically rather than appending to a string as a constant.

var age = "24"
var text = s"My age is $age"
println(text)

//Output
My age is 24

In the above String interpolation example, we have declared a dynamic string by “s” as a start letter and followed by String literal. We have referred to the age variable using the “$” symbol.

Let us see a few more examples.

var name = "Raj"
var age = "24"
var country = "Thailand"
var text = s"My name is $name and My age is $age and I live in $country"

//Output
My name is Raj and My age is 24 and I live in Thailand

We can use String “s” interpolation for boolean evaluation.

 var name = "Raj"
 var age = 24
 var text = s"My name is $name and is my age is greather than 24 ? ans is ${age > 20}"
 println(text)

//Output
My name is Raj and is my age is greather than 24 ? ans is true

In the above String interpolation example, to evaluate an expression we have to “${}” symbol.

“f” Interpolation

String “f” Interpolation allows us to format strings by appending spaces or format the decimal points.

var name = "Smith"
var age = "24"
Var text = f"$name%15s $age"
println(text)

//Output
          Smith 24

In the above example, we have appended spaces before the name.

var name = "Smith"
var age = "24"
var text = f"$name $age%15s"
println(text)

//Output
Smith              24

In the above example, we have appended spaces between the name and age.

“raw” Interpolation

String “raw” Interpolation does not apply line break or escape characters. It justs print the string as it is.

We will see how it works using “s” interpolation.

var name = "Smith"
var age = "24"
var country = "Thailand"
var text = s"$name\n$age\tcountry"
println(text)

//Output
Smith
24	country

It has applied both line break and tab characters to the string literals.

We will see how “raw” interpolation works for the same example

var name = "Smith"
var age = "24"
var country = "Thailand"
var text = raw"$name\n$age\tcountry"
println(text)

//Output
Smith\n24\tcountry

References: https://docs.scala-lang.org/overviews/core/string-interpolation.html

Thanks for reading. Please leave your comments and give us a thumbs up!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

11 thoughts on “Scala String Interpolation in detail

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.