You want to create multiline strings within your Scala source code, like you can with the heredoc syntax of other languages and help in escaping quotes and other symbols.
A heredoc
is a way to define a multiline string, while
maintaining the original indentation & formatting.This is used
to embed snippets of code, like SQL or HTML.
A great feature of Scala strings is that you can create multiline strings by including the string inside three double-quotes
:
val multiline = """First line starts
Second line
Third line ends."""
Although this works, the second and third lines in this example will end up with whitespace at the beginning of their lines. If you print the string, it looks like this:
First line starts
Second line
Third line ends
You can solve this problem in several different ways. First, you can left-justify every line after the first line of your string:
val multiline = """First line starts
Second line
Third line ends"""
A cleaner approach is to add the stripMargin
method to the end of your multiline string and begin all lines after the first line with the pipe symbol |
:
val multiline = """First line starts
|Second line
|Third line ends""".stripMargin
If you don’t like using the | symbol, you can use any character you like with the stripMargin method:
val multiline = """First line starts
#Second line
#Third line ends""".stripMargin(#)
All of these approaches yield the same result, a multiline string with each line of the string left justified:
First line starts
Second line
Third line ends
This results in a true multiline string, with a hidden \n character after the word “and” in the first line. To convert this multiline string into one continuous line you can add a replaceAll method after the stripMargin call, replacing all newline characters with blank spaces:
val multiline = """First line starts
|Second line
|Third line ends""".stripMargin.replaceAll("\n", " ")
This yields:
First line starts Second line Third line ends
Using triple quotes to escape characters
If you have used another programming language like Java or .NET in the past, you would be familiar with escaping quotes in a String using backslash \
val exampleJson: String = "{\"name\":\"xyz\",\"level\":\"high\",\"price\":2.50}"
println(s"exampleJson = $exampleJson")
You will see the below output if you run your Scala application.
exampleJson = {"name":"xyz","level":"high","price":2.50}
This is great but if you have longer text to escape, it will become very hectic to escape each and every individual quote within your JSON String.
Fortunately Scala has a much better solution! To help you easily escape characters and symbols inside strings, you just need to wrap the text within triple quotes
val exampleJson: String = """{"name":"xyz","level":"high","price":2.50}"""
println(s"exampleJson = $exampleJson")
You will see the same output if you run above code snippet.
exampleJson = {"name":"xyz","level":"high","price":2.50}
Like this:
Like Loading...
Pradeep Mishra
Share post:
You want to create multiline strings within your Scala source code, like you can with the heredoc syntax of other languages and help in escaping quotes and other symbols.
A great feature of Scala strings is that you can create multiline strings by including the string inside
three double-quotes
:Although this works, the second and third lines in this example will end up with whitespace at the beginning of their lines. If you print the string, it looks like this:
You can solve this problem in several different ways. First, you can left-justify every line after the first line of your string:
A cleaner approach is to add the
stripMargin
method to the end of your multiline string and begin all lines after the first line with the pipe symbol|
:If you don’t like using the | symbol, you can use any character you like with the stripMargin method:
All of these approaches yield the same result, a multiline string with each line of the string left justified:
This results in a true multiline string, with a hidden \n character after the word “and” in the first line. To convert this multiline string into one continuous line you can add a replaceAll method after the stripMargin call, replacing all newline characters with blank spaces:
This yields:
Using triple quotes to escape characters
If you have used another programming language like Java or .NET in the past, you would be familiar with escaping quotes in a String using backslash
\
You will see the below output if you run your Scala application.
This is great but if you have longer text to escape, it will become very hectic to escape each and every individual quote within your JSON String.
Fortunately Scala has a much better solution! To help you easily escape characters and symbols inside strings, you just need to wrap the text within triple quotes
You will see the same output if you run above code snippet.
Share this:
Like this:
Generate Sequential and Unique IDs in a Spark Dataframe
Apache Spark is an open source, general-purpose distributed computing engine used for processing and analyzing a large amount of data. Hence, adding sequential and unique IDs to a Spark Dataframe is not very straight forward, because of distributed nature of it.
Share this:
Like this:
Continue Reading
Spark Partitions with Coalesce and Repartition (hash, range, round robin)
One main advantage of the Apache Spark is, it splits data into multiple partitions and executes operations on all partitions of data in parallel which allows us to complete the job faster.While working with partition data we often need to increase or decrease the partitions based on data distribution. Methods repartition and coalesce helps us to repartition.
Share this:
Like this:
Continue Reading
Scala String Interpolation
Introduction String Interpolation refers to substitution of defined variables or expressions in a given String with respected values. String Interpolation allows users to embed variable references directly in processed string literals.. read more…
Share this:
Like this:
Continue Reading