Scala Multiline String

In real-life scenarios, we want to write multiple lines of string in the script. In the article, we have a great feature of Scala string to discuss. The feature is about creating multiline strings by comprising the strings in three double quotes and also with the stripMargin method. Although, there is no need for any string concatenation operator that spans multiple lines. The Scala Multiline string improves the readability of code.

How to Create Multiline String in Scala?

Strings in Scala are single lines that are wrapped into double quotes. We can create multiline strings in Scala by surrounding the text with three double quotes or using pipes through a stripMargin(). While creating the multiline string, you can control to maintain the original indentation and formatting.

Different Ways to Create Multiline String in Scala in Ubuntu 20.04?

To understand the different ways of creating the multiline string in Scala, we should take a look at the examples below:

Example # 1: Using Three double quotes (“ “ “)for Creating Multiline String

The first example is using three double quotes which help to write multiline strings in Scala. When we use three double quotes the space is preserved and displayed in the script. We have a sample code below that shows what the string looks like.

In the example code, we have created an object class as “Example1”. In the class main function, we have defined a variable with the keyword “val” named as “str1” which is initialized with the string. The string is kept in three double quotes as shown. The print statement is taking “str1” as a parameter that returns the multiline string.

Upon execution of the above code, we have the output of a multiline string. Note that the space is preserved here at the beginning of each line. We have to deal with these spaces and eliminate them. In the next example, we have a solution for these spaces.

Example # 2: Using StripMargin() for Creating Multiline String

In the above example code, we have a drawback of using three double codes as it keeps spaces preserved. We can eliminate all these spaces by using stripMargin at the end of the three double-quoted multiline strings and adding pipe(‘|’) symbols along with each line. With the stripMargin, we can eliminate the entire margin having blanks and separators from the string in the lists.

In the example code, we have declared a class as “Example2”. In the class “Example2”, we have defined our main function which is going to eliminate spaces. Here, we have a variable “val” as “str2”. The variable “str2” is initialized with the multiline string. This multiline string is kept inside the three double quotes and with the start of each line of string, we have used the pipe(“|”) symbol. Also, we have included stripMargin at the end of the string with no arguments passed. This removes the vertical bar from the beginning of the string line along with the whitespaces and blanks. In the end, we passed the multiline string which is stored in “str2” to the print function. The print function executes and displays the output.

After implementation and execution of the above example code, we have successfully removed all the space which is at the beginning of the string lines. The output is shown below on the terminal screen of Ubuntu 20.04.

Example # 3: Using Custom Token in StripMargin() for Creating Multiline String

If you don’t want to use the pipe(‘|’) symbol in the multiline string. Then Scala can use any other arbitrary character such that in our case, we use hashtag ‘#’ character. This symbol also eliminates whitespace from the beginning of each line. To use any other character in the multiline string, we have to pass that character in stringMargin() as an argument. Let’s execute the code in Ubuntu 20.04 to see how such arbitrary character removes the spaces.

In the example code, we have defined a class with the name “Example3”. For the class-main function, we have a variable “Val” defined as “str3”. The variable “str3” is initialized with the string wrapped in a three-double quote. Each line of multiline string we have included a hashtag character “#” that is going to unpreserved spaces around each new line. We have passed this hashtag “#” character in the stripMargin() which is added to the end of the string. Now, in the print function, we have passed “str3” which will return the multiline string with no spaces at the starting of the lines. Note that we have used the hashtag “#” in the single quote when passing it to stripMargin. If we pass a hashtag character in a double quote then, an exception occurs. The output multiline string returned upon execution of the Scala script which displays that there are no spaces at the beginning of the line. Hence, indentation is unpreserved.

Example # 4: Using replaceAll to Get a Single Line

To convert the multiline string into a single continuous string, we can add the replaceAll method at the end of the strip margin method. Below, we have a running example code that uses the replaceAll method to convert a multi-line string into a new single-line string.

The example code is declared with the class “Example4”. In the “Example4” class, we have initialized a variable “str4” with the multiline string. The multiline string is surrounded by the three double-quotes. At the end of the multiline string, we have the stripMargin method added along with the replaceAll method. The replaceAll method takes the line break “\n” symbol as an argument which will replace line breaks generated in the lines of the string. We have returned the value passed in the print function.

The single-line string has been fetched as from the replaceAll method.

Example # 5: Using Double Quotes(“”) Inside Expression

The single quotes and double quotes do not need to escape; that is another great feature in Scala multiline strings.

In the example code above, we have a class as “Example5” ]in which we have defined a variable with “str5”. The variable “str5” has a multiline string wrapped inside three double quotes. We have single-quoted the word “multi-line” and double-quoted the word “example” from the string. Now, print the multiline string which is stored in “str5”.


We have a deep discussion on the article Scala multi-line string. We have demonstrated different ways of creating multi-line strings in Scala by using three double quotes wrapped around the string and also using stripMargin which will eliminate the entire margin from the string. Then, we use pipe or some other character that provides consistency in both the indentation of the Scala script and the string. We hope you will get help from the Scala multi-line string article.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.