×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

String Interpolation in Scala

By IncludeHelp Last updated : October 20, 2024

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.

Rules for String Interpolation

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"

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 

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 

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 

String interpolation using expressions

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

String interpolation using function call

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.