1. Introduction

String manipulations are among the most fundamental operations in any programming language, and Scala provides various methods for achieving this. In this tutorial, we’ll examine different ways to concatenate strings in Scala. Concatenating strings involves merging two or more strings into one continuous string.

2. Using + Operator

The simplest way to concatenate two strings in Scala is by using the + operator:

val str1 = "Hello"
val str2 = "Baeldung"
val combined = str1 + str2
combined shouldBe "HelloBaeldung"

This is very straightforward and intuitive for beginners. This is an immutable operation, and the + operator creates another String instance with the concatenated value instead of modifying the existing strings. We can use the + operator to combine strings with other datatypes.

3. Using concat()

We can use the concat() function on the String to concat another string to it and create a new string:

val str1 = "Hello"
val str2 = "Baeldung"
val combined = str1.concat(str2)
combined shouldBe "HelloBaeldung"

The concat() method, like the + operator, performs an immutable operation by returning a new string. However, unlike the + operator, the concat() method can only combine another string with the input string and can’t be used with other types, such as Int. Furthermore, Scala provides an alias for the concat() function using the ++ operator:

val str1 = "Hello"
val str2 = "Baeldung"
val combined = str1 ++ str2
combined shouldBe "HelloBaeldung"

This works the same as the concat() function.

4. Using String Interpolation

String interpolation is a powerful feature in Scala that allows variables to be embedded directly into string literals. We can use this feature to combine multiple strings:

val str1 = "Hello"
val str2 = "Baeldung"
val combined = s"$str1$str2"
combined shouldBe "HelloBaeldung"

Here, we used the s-interpolator to combine the variables. This method can also concat multiple types such as Int, String, Double, etc.  Additionally, we can use a triple quote s-interpolator to create multi-line formatted strings.

5. Using StringBuilder

StringBuilder is an efficient option for more complex string manipulations and concatenation. It’s mutable and can handle large amounts of data without creating many intermediate objects:

val str1 = "Hello"
val str2 = "Baeldung"
val combined = new StringBuilder(str1).append(str2).toString()
combined shouldBe "HelloBaeldung"

Here, we created an instance of the StringBuilder and then appended different strings. Finally, we called the toString() method to create the String instance from it.

6. Using mkString()

In the previous sections, we combined strings individually. However, sometimes, we need to concatenate a list of strings. Instead of using loops, we can utilize the mkString() function on an Iterable, which can be applied to collections like Seq, List, and others:

val strings = Seq("Hello", "Baeldung")
val combined = strings.mkString
combined shouldBe "Hello,Baeldung"

Additionally, mkString() allows us to add separators between strings while combining them easily:

strings.mkString(",") shouldBe "Hello,Baeldung"

Here, we combined the string with a comma as the separator between them.

7. Using reduce()

Another way to combine a list of strings is to use the reduce() function:

val strings = Seq("Hello", "Baeldung")
val combined = strings.reduce(_ + _)
combined shouldBe "HelloBaeldung"

However, we should be aware that the reduce() method throws an exception if the input list is empty. Alternatively, we can use the foldLeft() function to safely operate even on an empty list:

val strings = Seq("Hello", "Baeldung")
val combined = strings.foldLeft("")(_ + _)
combined shouldBe "HelloBaeldung"
List.empty[String].foldLeft("")(_ + _) shouldBe ""

Here, we can see that it successfully combines strings, even on an empty list, using the foldLeft() function.

8. Conclusion

In this article, we explored various methods for concatenating strings in Scala, including the + operator, concat(), and string interpolation. We also discussed advanced techniques such as StringBuilder and mkString() for combining collections of strings. Additionally, we examined how reduce() and foldLeft() can be used to concatenate list elements. These methods offer flexibility in string manipulation in Scala, making it easy to choose the best option for different needs.

As always, the sample code used in this tutorial is available over on GitHub.


» 下一篇: ElasticMQ 简介