Scala Tutorial - Learn How To Use MkString Function With Examples
Overview
In this tutorial, we will learn how to use the mkString function with examples on collection data structures in Scala. The mkString function is applicable to both Scala's Mutable and Immutable collection data structures.
The mkString method will help you create a String representation of collection elements by iterating through the collection. The mkString method has an overloaded method which allows you to provide a delimiter to separate each element in the collection. Furthermore, there is another overloaded method to also specify any prefix and postfix literal to be preprended or appended to the String representation..
As per the Scala documentation, the definition of the mkString method is as follows:
def mkString: String
def mkString(sep: String): String
def mkString(start: String, sep: String, end: String): String
The mkString method is a member of the TraversableOnce trait.
Steps
1. How to initialize a Sequence of donuts
The code below shows how to initialize a Sequence of donut elements of type String.
println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)
2. How to concatenate the elements of a sequence into a String using mkString function
The code below shows how to use the mkString function to create a String representation for all the donut elements in the donut Sequence. In this example, we will concatenate the donut elements with the String " and ".
println("\nStep 2: How to concatenate the elements of a sequence into a String using mkString function")
val donutsAsString: String = donuts.mkString(" and ")
println(s"Donuts elements using mkString function = $donutsAsString")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to concatenate the elements of a sequence into a String using mkString function
Donuts elements using mkString function = Plain Donut and Strawberry Donut and Glazed Donut
3. How to concatenate the elements of a Sequence into a String using mkString and specifying prefix and suffix
As mentioned above, the mkString method has an overloaded method signature which allows you to specify a prefix and suffix to your String presentation. In the example below, we will use the String "My favorite donuts namely " as prefix and the String " are very tasty!" as the suffix.
println("\nStep 3: How to concatenate the elements of a sequence into a String using mkString and specifying prefix and suffix")
val donutsWithPrefixAndSuffix: String = donuts.mkString("My favorite donuts namely ", " and ", " are very tasty!")
println(s"$donutsWithPrefixAndSuffix")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to concatenate the elements of a sequence into a String using mkString and specifying prefix and suffix
My favorite donuts Plain Donut and Strawberry Donut and Glazed Donut are very tasty!
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to concatenate the elements of a sequence into a String using mkString function
- How to concatenate the elements of a Sequence into a String using mkString and specifying prefix and suffix
Tip
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use nonEmpty function.