s-, f- und Raw-Interpolatoren
Binden Sie Werte in Strings ein.
s-, f- und Raw-Interpolatoren ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „s-, f- und Raw-Interpolatoren“ kostenlos?
Ja — der vollständige Text von „s-, f- und Raw-Interpolatoren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „s-, f- und Raw-Interpolatoren“?
Binden Sie Werte in Strings ein. Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?
Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „s-, f- und Raw-Interpolatoren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?
Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Grundlagen von Strings
- s-, f- und Raw-Interpolatoren
- Häufige String-Methoden
- Mehrzeilige Strings