s、f 和原始插值器
在字符串中嵌入值。
s、f 和原始插值器 是 CoddyKit 上的免费 Scala for Backend Engineering & Functional Programming 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Scala for Backend Engineering & Functional Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Scala for Backend Engineering & Functional Programming 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is Interpolation?
String interpolation lets you embed values directly inside a string literal.
Instead of joining pieces with +, you put a prefix letter before the quotes and reference variables inside. Scala has three built-in interpolators: s, f, and raw.
val name = "Ada"
println(s"Hello, $name!")The s Interpolator
The s interpolator is the most common. Put s before the opening quote, then use $variable to insert a value.
It reads more clearly than concatenation with +.
val count = 3
println(s"You have $count messages")Expressions with Braces
For anything more than a simple variable, wrap the expression in ${ }.
This lets you do arithmetic or call methods right inside the string.
val a = 4
val b = 5
println(s"Sum is ${a + b}")
println(s"Upper: ${"hi".toUpperCase}")A Runnable s Example
Here is a complete program using the s interpolator to build a greeting.
Notice how the variables are inserted neatly into the sentence.
object Main extends App {
val user = "Sam"
val items = 2
println(s"Hi $user, you bought $items items.")
}Escaping a Dollar Sign
Inside an interpolated string, $ has special meaning. To print a literal dollar sign, write $$.
This is useful when formatting prices or currency.
val price = 9
println(s"Total: $$$price")The f Interpolator
The f interpolator adds printf-style formatting. After a variable, add a format specifier like %d for integers or %s for strings.
It is great for controlling number precision and alignment.
val n = 42
println(f"Value: $n%d")
val label = "Pi"
println(f"$label%s")Formatting Decimals
Use %f with a precision to control decimal places. For example %.2f shows two digits after the point.
This is ideal for money or measurements.
val pi = 3.14159
println(f"Pi is $pi%.2f")
val ratio = 0.5
println(f"Ratio: $ratio%.1f")Width and Padding with f
The f interpolator can also pad numbers to a width. %5d right-aligns an integer in a field five characters wide.
This helps line up columns of output.
val x = 7
println(f"[$x%5d]")
println(f"[$x%-5d]")The raw Interpolator
The raw interpolator works like s but does not process escape sequences.
So \n stays as the two characters backslash and n, instead of becoming a new line.
println(raw"Path: C:\new\test")
println(s"Path: C:\new\test")Choosing the Right One
Use s for everyday value insertion. Use f when you need number formatting. Use raw when you want backslashes kept literally, like file paths or regex.
All three still support $variable and ${expression}.
val v = 1.0
println(s"s: $v")
println(f"f: $v%.2f")
println(raw"raw: $v\n")Multiple Values Together
You can combine many variables and expressions in one interpolated string.
This keeps output code compact and easy to read compared to long chains of +.
object Main extends App {
val item = "Coffee"
val qty = 2
val cost = 4.5
println(f"$qty x $item = $$${qty * cost}%.2f")
}Quick Check
Which interpolator keeps escape sequences literal?
Recap
You learned Scala's three interpolators. s inserts values with $var and ${expr}.
f adds printf-style formatting like %.2f and %5d. raw behaves like s but keeps escape sequences literal. Use $$ to print a real dollar sign.
常见问题解答
「s、f 和原始插值器」课时是免费的吗?
是的 — 「s、f 和原始插值器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Scala for Backend Engineering & Functional Programming 课程的其余内容,请升级到 CoddyKit PRO。 Scala for Backend Engineering & Functional Programming 课程共包含 4 节课。
「s、f 和原始插值器」这节课中我会学到什么?
在字符串中嵌入值。 你通过在浏览器中直接运行的动手代码来练习 Scala for Backend Engineering & Functional Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Scala for Backend Engineering & Functional Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Scala for Backend Engineering & Functional Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「s、f 和原始插值器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Scala for Backend Engineering & Functional Programming 课中编写并运行代码吗?
能。每节 Scala for Backend Engineering & Functional Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。