Home » Scala language

String Interpolation in Scala

String interpolation is defining a string with variables or expressions in between it. In this tutorial on string interpolation we will learn about Scala string interpolation in detail with working examples.
Submitted by Shivang Yadav, on October 03, 2019

Scala String Interpolation

String Interpolation is defining a string literal with one or more placeholders, these placeholders are to be replaced with variables or expressions while the final compilation of the result.

Scala programming languages support string interpolation and multiple methods to process the string literals easily.

There are a few rules that are to be kept in mind while using the concept of string interpolation in Scala to yield proper and error less results. The rules are:

  • The defined string must be a starting with a s / f / raw keywords.
  • All the variables and expressions that are to be used as placeholders should have a prefix $.
  • For placing an expression in placeholder, the expression should be enclosed within curly braces {}.

Syntax:

    val str = s"Hello welcome to $name"

1) String Interpolation using "s" keyword

The "s" keyword is added at the start of the string in which we intend to add placeholders for using variables or expressions.

Syntax:

    val str = s"string ${expression}"

Example:

object MyClass {
    def main(args: Array[String]) {
        var bikename = "KTM Duke 390"
        var topspeed = 185 
        var str = s"I have $bikename and it has a top speed of $topspeed "
        println(str)
    }
}

Output

I have KTM Duke 390 and it has a top speed of 185 

2) String Interpolation using "f" Keyword

The "f" keyword is added at the start of the string to which we wish to add variables and expressions.

Syntax:

    val str = f"string ${expression}"

Example:

object MyClass {
    def main(args: Array[String]) {
        var bikename = "ThunderBird 350"
        var topspeed = 137
        var str = f"I have $bikename and it has a top speed of $topspeed "
        println(str)
    }
}

Output

I have ThunderBird 350 and it has a top speed of 137 

3) String Interpolation using "raw" keyword

The "raw" keyword is added at the start of the string to which we wish to add variables and expressions.

Syntax:

    val str = raw"string ${expression}"

Example:

object MyClass {
    def main(args: Array[String]) {
        var bikename = "Ninja 300"
        var topspeed = 150
        var str = raw"I have $bikename and it has a top speed of $topspeed "
        println(str)
    }
}

Output

I have Ninja 300 and it has a top speed of 150 

Using expressions in String Interpolation

Expressions can also be used in the place of placeholders in Scala programming language.

Syntax:

    val str = s"string $(expression)"

Example:

object MyClass {
    def main(args: Array[String]) {
        var a = 34
        var b = 75
        var str = s"The product of $a and $b is ${a*b}"
        println(str)
    }
}

Output

The product of 34 and 75 is 2550

Using function call in String Interpolation

In Scala programming language, you can also call a method and use its value in the placeholder place.

Syntax:

    val str = s"string$(functioncall)"

Example:

object MyClass {
    def add(x:Int, y:Int) = x + y;
    
    def main(args: Array[String]) {
        var x = 534;
        var y = 21;
        print(s"sum of $x + $y =  ${add(x,y)}");
    }
}

Output

sum of 534 + 21 =  555



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.