Scala

Scala String Interpolation

String interpolation in the Scala programming language refers to the process of replacing certain variables or expressions within a string with some other suitable value according to the context. This process is used extensively to deal with strings in the Scala programming language.

Therefore, in this article, we will discuss all the different string interpolators of the Scala programming language followed by their respective usage in Ubuntu 20.04.

What Are the Different String Interpolators?

There are three different string interpolators used within the Scala programming language. The details of these three string interpolators with the purposes for which they are used are explained below:

  • The “s” String Interpolator – This string interpolator treats the given string in Scala as a processed string. This is the most commonly used string interpolator.
  • The “f” String Interpolator – This string interpolator of the Scala programming language treats the strings as a formatted string. This string interpolator is especially helpful for processing the strings containing numbers.
  • The “raw” String Interpolator – This string interpolator is used for printing the escape sequences used within a string, such as “\n”. It means that if you use the raw string interpolator with a string that contains this escape sequence, then the “\n” escape sequence will be printed as it is instead of printing a new line.

Using the Different String Interpolators in Scala in Ubuntu 20.04

To learn the usage of the different string interpolators in the Scala programming language in Ubuntu 20.04, you will have to look at the following three examples:

Example # 1: Using the “s” String Interpolator in Scala

In this example, we will be using the “s” string interpolator in a very basic Scala script. The exact script is as follows:

In this example, we have created a class named “StInter” which represents the string interpolation class. Then, within this class, we have a “main()” function in which we have declared a value named “Diana”. Then, we wanted to print a greeting message for this name.

First, we have notified the user about the functionality of our program through a message. Then, we have used the “s” string interpolator within our “println” function followed by a greeting message and the notation “$name”. This will automatically treat our name as a string and will print the greeting message followed by that name on the terminal.

For compiling this Scala program, we have used the command provided below:

$ scalac StInter.Scala

Then, for running this Scala program, we have used the command that follows:

$ scala StInter

The output of this Scala program is shown in the image below. It confirms that the “s” string interpolator has correctly processed our “name” string.

Example # 2: Using the “f” String Interpolator in Scala

In this Scala program, we will learn to use the “f” string interpolator in Scala, which is used for processing the format strings correctly. However, to understand their process in the true sense, you must see the following Scala script:

In this Scala script, we have defined a value named “weight” and assigned an arbitrary weight, i.e., 35.5 in this case. We know that since this weight is in floating-point numbers and to process it correctly, we must use the “%.2f” notation. However, we will print this weight without the “f” string interpolator. Then, we will print it with the “f” string interpolator to compare the two outputs. Therefore, we have used two different “println” statements.

When we compiled and executed this Scala program, the output shown in the following image is displayed on the terminal. The first line in the output shows the weight printed without using the “f” string interpolator. Whereas the second line shows the weight printed using the “f” string interpolator. Because of this, we could not receive the correct output by executing the first “println” statement. However, on the second time, we managed to print the weight correctly.

Example # 3: Using the “raw” String Interpolator in Scala

Finally, in this example, we will learn to use the “raw” string interpolator in the Scala programming language. For that, you can take a look at the following Scala code:

In this Scala code, we have defined a value named “message1” and assigned a string containing two words separated by the escape sequence “\n”. This escape sequence is used to introduce a new line. Then, we have defined another value named “message2”. Again, we have assigned the same string with the same escape sequence. Note, we have used the “raw” string interpolator before this string. We have defined these two strings so that we can draw a comparison between a string printed with a raw string interpolator and a string without the raw string interpolator. After that, we have simply used two “println” statements for printing these strings on the terminal.

The following image displays the output of our Scala script. You can see that for the first time, when we did not use the “raw” string interpolator with our message, the “\n” escape sequence correctly introduced a new line to the output. However, when we used the “raw” string interpolator with our message, the “\n” escape sequence was printed as it is in the output without introducing any new line in the output.

Conclusion

This article was completely based on the discussion of string interpolation in the Scala programming language in Ubuntu 20.04. We discussed all the three different string interpolators used in the Scala programming language and the purposes for which they are used. After that, we explained three different examples, each of which we used one of these string interpolators of the Scala programming language. Once you understand these examples, you can use all three string interpolators in the Scala programming language. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.