Scala String Interpolation | s String, f String and raw string Interpolator

Free Scala course with real-time projects Start Now!!

Scala String Interpolation

Today in Scala String Interpolation tutorial, we will discuss the string interpolators ‘s’, ‘f’, and ‘raw’ with their examples.

So, let’s start Scala String Interpolation Tutorial.

Scala String Interpolation | s String, f String and raw string Interpolator

Scala String Interpolation | s String, f String and raw string Interpolator

Introduction to Interpolation in String

Scala String Interpolation is a new way to create them. But we didn’t always have this feature at hand. This was introduced in Scala 2.10, and is available in all newer versions as well.

By interpolating a string, we can embed variable references directly in a processed string literal. We have three kinds of Scala String interpolators. Let’s see them one by one.

s String Interpolator

When we prepend ‘s’ to any string, we can directly use variables in it with the use of the ‘$’ character. But that variable should be in scope in that string; it should be visible. Well, ‘s’ is a method.

scala> val name="Ayushi"
name: String = Ayushi
scala> println(s"Hello, $name, how are you?")
Hello, Ayushi, how are you?
See how simple that was? We can also process arbitrary expressions using ${}.
scala> println(s"Hello, 2+3 is ${2+3}")

Hello, 2+3 is 5

scala> println(s"Is 2+3 equal to 5? That is ${2+3==5}")

Is 2+3 equal to 5? That is true

We’ll also see this with case classes later.

f String Interpolator

Let’s first see the problem with the s interpolator.

scala> val a=77.000

a: Double = 77.0

scala> println(s"The number is $a")

The number is 77.0

This prints 77.0, not 77.000.

Now, see this with the f interpolator.

scala> println(f"The number is $a%.3f")

The number is 77.000

This is quite like the printf style format specifiers in C. These are %d, %i, %f, and so on.

scala> println(f"$name%s says the number is $a%.3f")

Ayushi says the number is 77.000

This is type-safe. If the variable reference and the format specifier don’t match, it raises an error. By default, the specifier is %s. Next in Scala String Interpolation is raw String Interpolator.

raw String Interpolator

raw is like s, except that it doesn’t escape literals within a string.

scala> println(raw"$name says the number is $a\nOkay, bye")

Ayushi says the number is 77.0\nOkay, bye

The same with s will give:

scala> println(s"$name says the number is $a\nOkay, bye")

Ayushi says the number is 77.0

Okay, bye

This tells us that the raw interpolator interpolates the string in a very raw sense.

So, this was all about Scala String Interpolation. Hope you like our explanation.

Conclusion

Hence, we discussed three types of Scala string interpolators: s String Interpolator, f String Interpolator, and raw String Interpolator in Scala with their examples. Furthermore, if you have any query, feel free to ask in the comment session.

Reference

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

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