2

String Interpolation in Scala

 2 years ago
source link: https://blog.knoldus.com/string-interpolation-in-scala-3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Reading Time: 2 minutes

Lets just refresh few of the Scala concepts in more detail, So in this blog we are going to discuss about string interpolation which is a amazing concept. As we know logging, printing and debugging is an important part of programming by String Interpolation we can use the String literals in an easy way.

String Interpolation

String interpolation is a concept in which we can replace a defined variable and expression in a value or We can say String Interpolation allows users to embed variable references directly in processed string literals.

We have some rules for defining string interpolation in Scala:

  • Strings starting with s or f or raw letter
  • Variables in that String with $variable_name syntax
  • Expressions in that String with ${expression} syntax
  • Define Object fields with ${object.field} syntax

Types of string interpolation

Scala supports mainly three kinds of String Interpolation:-

  • s String Interpolation
  • f String Interpolation
  • raw String Interpolation

The s String Interpolation

We use s in the starting of any string and after that in a string we can user values :-

scala> val name ="joe"
name: String = joe
scala>  println(s"Name of the student is $name")
Name of the student is joe

here in the above example $name is being inserted with the help of String Interpolation .

First we have defined a variable name and after that in a string that variable is being inserted with the help of s String interpolation.

The f String interpolation

In this String Interpolation, we should define String Literals with f String Interpolation, which means String Literal should start with ‘f’ letter.

f String interpolation is used to format the data

scala> val number = 7.9
number: Double = 7.9
scala> println(s"The number is formatted as $number%.3f")
The number is formatted as 7.9%.3f
scala> println(f"The number is formatted as $number%.3f")
The number is formatted as 7.900

The f interpolator makes use of the string format utilities available from Java. If there is no % character after a variable definition a formatter of %s (String) is assumed.

scala> println(f"The number is formatted as $number")
The number is formatted as 7.9

raw String interpolation

In raw String interpolation we use raw in the starting same as s and f

Suppose in a string literal we are using \n new line character in that situation it will be considered as a new character

scala> val escape = s"a\nb\nc"
escape: String =
 a
 b
 c

but with raw String interpolation

scala> val escape = raw"a\nb\nc"
escape: String = a\nb\nc

Scala’s String Interpolation in Java

When we compare String Interpolation of Scala in Java, Java doesn’t support String Interpolation, but java have ‘printf’ function which works similar to this.

Example:-

String playerName = "Ram";
float score = 95.5;
System.out.printf("Player Details: %s %d.2f", playerName, score);

Output:-

Player Details: Ram 95.50

Scala’s f String Interpolation is similar to Java’s printf function.

Hope this blog will help you.

Happy Coding!!!

References:-


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK