Topic Overview and Actualization

Are you ready to delve deeper into Scala's constructs? Today, we're focusing on string concatenation and interpolation. These operations form the backbone of string manipulation in Scala. We'll start from the basics and work our way up to practical applications. Ready to dive in? Let's start!

Introduction to String Concatenation

As in natural languages where words combine to form sentences, in Scala, smaller strings can be concatenated to form larger strings. You can use + for this purpose. Let's see this operation in action:

@main def run: Unit =
  val firstName = "John"
  val lastName = "Doe"
  val fullName = firstName + " " + lastName  // Concatenate strings
  println(fullName)  // John Doe
Introduction to String Interpolation

While concatenation merges whole strings, interpolation embeds values or variables into strings. Scala makes this a breeze with the s string interpolator:

@main def run: Unit =
  val userName = "John Doe"
  val greeting = s"Hello, $userName"  // User name interpolated in the string
  println(greeting)  // Hello, John Doe

Scala also allows expressions within interpolated strings:

@main def run: Unit =
  val apples = 5
  val oranges = 10
  val message = s"You have ${apples + oranges} fruits in total."  // Expression interpolated in the string
  println(message)  // You have total 15 fruits.
Escaping Characters and Using Raw Strings

Sometimes, you may need to include special characters in your strings that have particular meanings in Scala. For instance, the double quote " or the backslash \. In order to use these characters as string content, you need to escape them with the backslash \:

@main def run: Unit =
  val quote = "He said, \"Hello, World!\""  // Using escape character to include double quotes in the string
  println(quote)  // He said, "Hello, World!".

You might notice that using a lot of escape characters can make a string hard to read. Scala provides an alternative way to express strings, called raw strings, that does not interpret escape characters. Raw strings are represented using three double quotes """:

@main def run: Unit =
  val filePath = """C:\Users\John\Documents"""  // Backslashes do not need to be escaped in a raw string
  println(filePath)  // C:\Users\John\Documents

In raw strings, all characters are considered literals. This feature is handy when working with file paths, regular expressions, and multiline strings:

@main def run: Unit =
  val poem = """Roses are red,
Violets are blue,
Scala is awesome,
And so are you!"""  // Multiline string using raw strings
  println(poem)
  // Roses are red,
  // Violets are blue,
  // Scala is awesome,
  // And so are you!

In order to include variables in raw strings, you need to use s""" in combination with triple double quotes:

@main def run: Unit =
  val userName = "John Doe"
  val filePath = s"""C:\\Users\\$userName\\Documents"""  // Using `s` to include variables in raw strings
  println(filePath)  // C:\Users\John Doe\Documents

In this example, the backslashes \ need to be escaped (\\) because the s interpolator is used. Even though you're using triple double quotes """ for a raw string, the presence of the s interpolator changes this behavior. The s interpolator processes the string for Scala expressions and escape sequences, requiring backslashes to be escaped to represent them literally.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal