String Concatenation
In this page:
String Concatenation
The + operator joins strings together, and Scala automatically converts non-string operands to their string form when one side is already a String. While + works fine for simple cases, string interpolation is generally preferred for building strings with multiple embedded values, since it's more readable. Both approaches ultimately produce a new immutable String.
Note: For more than a couple of concatenated pieces, string interpolation (s"...") reads more clearly than chained + operators.
Example: String Concatenation
object Main extends App {
val first = "Hello"
val second = "Scala"
val combined = first + ", " + second + "!"
println(combined)
println("Count: " + 42)
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: