Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, there are several ways to concatenate strings. Some of the most common methods are:
+
Operator:The simplest way to concatenate strings is using the +
operator.
val str1 = "Hello" val str2 = "World" val result = str1 + " " + str2 // "Hello World"
Scala provides string interpolation, which allows embedded expressions inside string literals.
s-interpolator:
val name = "Alice" val greeting = s"Hello, $name!" // "Hello, Alice!"
With complex expressions:
val x = 10 val y = 20 val sum = s"The sum of $x and $y is ${x + y}." // "The sum of 10 and 20 is 30."
f-interpolator (similar to printf in other languages):
val weight = 68.5 val message = f"I weigh $weight%.2f kg." // "I weigh 68.50 kg."
concat
method:The concat
method is a direct way to concatenate strings.
val str1 = "Hello" val str2 = "World" val result = str1.concat(" ").concat(str2) // "Hello World"
mkString
with Sequences:If you have a sequence of strings and want to concatenate them, you can use mkString
.
val words = Seq("Hello", "World") val result = words.mkString(" ") // "Hello World"
StringBuilder
:For performance-sensitive scenarios where there's a lot of string manipulation, using StringBuilder
can be more efficient.
val sb = new StringBuilder sb.append("Hello").append(" ").append("World") val result = sb.toString // "Hello World"
String.format
:This is similar to Java's String.format
method.
val formatted = String.format("Hello, %s!", "Alice") // "Hello, Alice!"
For simple concatenations or when you need to embed variables within strings, string interpolation (s"..."
) is the most idiomatic way in Scala.
If you're dealing with sequences of strings, mkString
is very handy.
For performance-critical applications with a lot of string manipulation, consider StringBuilder
.
In general, choose the method that makes your code most readable and maintainable for your specific use case.
Efficient string concatenation in Scala:
StringBuilder
, String.concat
, mkString
, or StringBuffer
for mutable concatenation.val strings = List("Hello", " ", "Scala", " ", "World") // Using mkString val result = strings.mkString
Concatenating strings with + operator in Scala:
+
operator can be used to concatenate strings in Scala. However, it can be inefficient for repeated concatenations due to string immutability.val str1 = "Hello" val str2 = " Scala" val result = str1 + str2
StringBuilder vs String concatenation in Scala:
StringBuilder
is often more efficient than string concatenation using the +
operator, especially in loops or repeated concatenations.val str1 = "Hello" val str2 = " Scala" // Using StringBuilder val resultBuilder = new StringBuilder(str1).append(str2).toString() // Using + val resultPlus = str1 + str2
String interpolation in Scala:
val name = "Scala" val message = s"Hello, $name!"
Concatenating strings using concat method in Scala:
concat
method is available in Scala for string concatenation. It is similar to the +
operator but creates a new string.val str1 = "Hello" val str2 = " Scala" val result = str1.concat(str2)
Joining strings with mkString in Scala:
mkString
method is used to concatenate elements of a collection into a single string. It allows specifying a separator.val strings = List("Hello", "Scala", "World") val result = strings.mkString(" ")
Immutable string concatenation in Scala:
val str1 = "Hello" val str2 = " Scala" val result = str1 + str2