Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Scala - String Interpolation - Testingpool

Scala – String Interpolation

In the previous post, we have learnt about string literal and its basic functions in scala. In this post, we will see a concept called String interpolation introduced in scala version 2.10 that helps in processing the string in a better way similar to other languages like Perl, PHP and Ruby.

What is string Interpolation?

As we know that string is a sequence of characters and sometimes we can have a string as combination of characters, variables or expressions. Scala provides a way to replace this variables or expressions with expected values and this concept is called String Interpolation.

Types of String Interpolation:

String interpolation are having following categories.

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

We will understand them one by one with examples.There are some rules that we need to follow to work with string Interpolation.

Rules to follow:

  • String must be started with s/f/raw.
  • Each variable or expression will have a ‘$’ as prefix.
  • Expression must be enclosed within curly braces {}.

Let’s start with s string interpolation and understand it with an example. The most common use of string interpolation can be seen as string concatenation. If you will look at the below example where we are concatenating ‘hello‘ with name variable.

scala> val name="Tom"
name: String = Tom

scala> print(s"Hello ,$name")
Hello ,Tom

s String Interpolator:

This interpolator can be used to access variable, object ,function calls etc.

Example1: Access variable in String literal

We are defining variables as firstname and lastname and using s interpolator to use it in string starting with s.

scala> val firstname="Tom"
firstname: String = Tom

scala> val lastname="Hanks"
lastname: String = Hanks

scala> println(s"Actor name: $firstname $lastname")
Actor name: Tom Hanks

Example2: Access object fields in string literals

scala> class actorName(val firstname:String,val lastname:String)
defined class actorName

scala> val actor = new actorName("Tom","Hanks")
actor: actorName = actorName@7b944285

scala> val str = s"Actor Name: ${actor.firstname} ${actor.lastname}"
str: String = Actor Name: Tom Hanks

Example3: Access function call in string literal

scala> class actorName(val firstname:String,val lastname:String){
     | def printName = firstname + " " + lastname
     | }
defined class actorName

scala> val actor = new actorName("Tom","Hanks")
actor: actorName = actorName@6c7186f2

scala> val str = s"Actor name: ${actor.printName}"
str: String = Actor name: Tom Hanks

Example4: Handle expression in string literal

scala> val str = s"Addition of 4 and 5:- ${4 + 5}"
str: String = Addition of 4 and 5:- 9

f String Interpolator:

This interpolator helps in number formatting. To use it , we place the instructions after the expression , starting with percent sign (%). Suppose , we have a weight given as 100.0 and we wan to add more decimal to it or completely remove it. How can we do that? We will use f string interpolator to achieve this.

scala> val weight = 200.0
weight: Double = 200.0

//Without f string interpolator
scala> val twoDecimal = "weight is given as $weight%.2f"
twoDecimal: String = weight is given as $weight%.2f

//using f string interpolator
scala> val twoDecimal = f"weight is given as $weight%.2f"
twoDecimal: String = weight is given as 200.00

scala> val noDecimal = f"weight is given as $weight%.0f"
noDecimal: String = weight is given as 200

raw String Interpolator:

When we don’t want to evaluate escape character, we should use raw string interpolator. If we have \n in the string literal and it starts with raw, then it will accept \n as it is. That means it does not evaluate \n as new line character.

scala> val str = s"Hello \n there!!"
str: String =
Hello
 there!!

scala> val str =raw"Hello \n there!!"
str: String = Hello \n there!!
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...