Introduction
Starting in Scala 2.10.0, Scala offers a new mechanism to create strings from your data: String Interpolation. String Interpolation allows users to embed variable references directly in processedstring literals. Here’s an example:val name = "James"println(s"Hello, $name") // Hello, James
s"Hello, $name" is a processed string literal. This means that the compiler does some additional work to this literal. A processed string literal is denoted by a set of characters precedding the ". String interpolation was introduced by SIP-13, which contains all details of the implementation.Usage
Scala provides three string interpolation methods out of the box:s, f and raw.
The s String Interpolator
Prepending s to any string literal allows the usage of variables directly in the string. You’ve already seen an example here:val name = "James"println(s"Hello, $name") // Hello, James
$name is nested inside an s processed string. The s interpolator knows to insert the value of the name variable at this location in the string, resulting in the string Hello, James. With the sinterpolator, any name that is in scope can be used within a string.String interpolators can also take arbitrary expressions. For example:
println(s"1 + 1 = ${1 + 1}")
1 + 1 = 2. Any arbitrary expression can be embedded in ${}.
The f Interpolator
Prepending f to any string literal allows the creation of simple formatted strings, similar to printfin other languages. When using the f interpolator, all variable references should be followed by aprintf-style format string, like %d. Let’s look at an example:val height = 1.9dval name = "James"println(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall
f interpolator is typesafe. If you try to pass a format string that only works for integers but pass a double, the compiler will issue an error. For example:val height: Double = 1.9dscala> f"$height%4d"<console>:9: error: type mismatch;found : Doublerequired: Intf"$height%4d"^
f interpolator makes use of the string format utilities available from Java. The formats allowed after the % character are outlined in the Formatter javadoc. If there is no % character after a variable definition a formatter of %s (String) is assumed.
The raw Interpolator
The raw interpolator is similar to the s interpolator except that it performs no escaping of literals within the string. Here’s an example processed string:scala> s"a\nb"res0: String =ab
s string interpolator replaced the characters \n with a return character. The rawinterpolator will not do that.scala> raw"a\nb"res1: String = a\nb
\n turn into a return character.In addition to the three default string interpolators, users can define their own.
example:
package com.sagar.basic
import scala.math._
object Hello extends App {
println("Hello World");
//String Interpolation Example
val Tau = Pi * 2
println(s"Valie of tau is $Tau");
//String Interpolation example with s & f
println("Result for string interpolation with s:" + s"Happy $Tau Day")
println("Result for string interpolation with f:" + f"Happy $Tau%2.2f Day")
//String Formating
printf("Now you have %.7f problems.", Math.nextAfter(2.0, 3))
}
No comments:
Post a Comment