×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala StringContext Class

By IncludeHelp Last updated : November 01, 2024

StringContext Class

The stringContext class in Scala is used to support string interpolation. The class extends the product with serializable. And has linear supertypes Serializable, java.io.Serializable, Product, Equals, AnyRef, Any.

String Interpolation Members

The class has the following members that support string interpolation:

  1. Formatted string interpolater: macro def f[A >: Any] (argument : A*) : String
  2. Raw string interpolater: def raw(argument: Any*) : String
  3. Simple string interpolater: def s(argument: Any* ): String

All three functions are used for string interpolation. And are used to add variables between the formatted strings.

Let's see each of the methods in action to interpolate strings.

Formatted string interpolater

It is used to add the given arguments (variables/ expressions) in between the string. At the specified positions. 

Syntax

macro def f[A >: Any] (argument : A*) : String

Arguments

It can take any number of arguments that will be inserted in the string. The variable after $ is arguments.

Return Type

It returns a string which is interpolated string.

Example

object MyClass {
    def strPrint(proglang : String , year : Int){
        printf(f"$proglang was released in the year $year.\n")
    }
        
    def main(args: Array[String]) {
        strPrint("Scala", 2004)
        strPrint("JavaScript", 1995)
    }
}

Output

Scala was released in the year 2004.
JavaScript was released in the year 1995.

In the above code, we have created a function strPrint() which accepts two arguments a string and an integer value. And interpolate them to a string in the following way:
proglang + " was released in the year " + year + "." and prints the result based on the arguments.

2) Raw string interpolator

It is used to insert the given arguments (variables) in their places defined in the string.

Syntax

def raw(argument: Any*) : String

Arguments

It can take any number of arguments that will be inserted in the string. The variable after $ is arguments.

Return Type

It returns a string which is interpolated string.

Example

object MyClass {
    def RawStrInterpolator(employee : String , salary : Int){
        println(StringContext(""," gets a salary of Rs.", ".").raw(employee , salary))
    }
        
    def main(args: Array[String]) {
        RawStrInterpolator("Sudhir", 175000)
        RawStrInterpolator("Ramesh", 50000)
    }
}

Output

Sudhir gets a salary of Rs.175000.
Ramesh gets a salary of Rs.50000.

In the above code, we have created a method RawStrInterpolator() that demonstrates the functioning of Raw String Interpolator. The method accepts two arguments named employee and salary and interpolates then to the string using the raw method. The output of the interpolated string is of the form :
employee + " gets a salary of Rs." + salary + ".". And then prints the output based on the arguments.

3) Simple string interpolator

This method is used to add the given arguments in their places in the given string.

Syntax

def s(argument: Any*) : String

Arguments

It can take any number of arguments that will be inserted in the string. The variable after $ is arguments.

Return Type

It returns a string which is interpolated string.

Example

object MyClass {
    def simpleStrInterpolator(principal : Int , rate : Int){
        println(s"The interest on $principal at the rate of $rate for 1 year is ${(principal*rate*1)/100 }")
    }
        
    def main(args: Array[String]) {
        simpleStrInterpolator(175000, 8)
        simpleStrInterpolator(50000, 10)
    }
}

Output

The interest on 175000 at the rate of 8 for 1 year is 14000
The interest on 50000 at the rate of 10 for 1 year is 5000

In the above code, we have created a function simpleStrInterpolator() which interpolates the string using s method. We have passed two arguments to the string which are principal and rate. The output of the interpolated string is of the form:
"The interest on" + principal + "at the rate of" + rate + "for 1 year is" + {(principal*rate*1)/100 }

The last expression will be solved and calculated value will be printed.

Comments and Discussions!

Load comments ↻





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