Scala Lesson 6 – Strings | Dataplexa

Strings in Scala

In this lesson, you will learn about Strings in Scala. Strings are used to store text data such as names, messages, file paths, and user input.

Scala strings are immutable, which means once a string is created, it cannot be changed. Any operation on a string produces a new string instead of modifying the original one.


What Is a String?

A string is a sequence of characters enclosed in double quotes. Scala uses the String type from Java, but extends it with powerful features.

val language: String = "Scala"
println(language)

Here, the variable language stores text data.


Creating Strings

You can create strings in Scala in multiple ways.


Using Double Quotes

val name = "Dataplexa"
println(name)

This is the most common way to create a string.


Multiline Strings

Scala supports multiline strings using triple quotes. This is useful for long text, messages, or documentation.

val message =
  """Welcome to Dataplexa
    |Learn Scala step by step
    |Build real projects""".stripMargin

println(message)

The stripMargin method removes extra indentation.


String Concatenation

String concatenation means joining multiple strings together.


Using + Operator

val firstName = "Scala"
val lastName = "Developer"

val fullName = firstName + " " + lastName
println(fullName)

The + operator joins strings and text values.


String Interpolation (Very Important)

String interpolation allows embedding variables directly inside strings. This is one of the most powerful features in Scala.


S-Interpolator

val course = "Scala"
val level = "Beginner"

println(s"$course Course - $level Level")

The s before the string allows variables to be injected using $.


Expression Interpolation

You can also evaluate expressions inside interpolated strings.

val price = 100
val tax = 18

println(s"Total price: ${price + tax}")

Expressions are written inside ${ }.


Common String Methods

Scala provides many built-in methods to work with strings.


Length of a String

val text = "Scala"
println(text.length)

This returns the number of characters in the string.


Convert to Uppercase and Lowercase

val word = "Scala"

println(word.toUpperCase)
println(word.toLowerCase)

These methods change the case of characters.


Checking Substrings

val sentence = "Learning Scala is fun"

println(sentence.contains("Scala"))
println(sentence.startsWith("Learning"))
println(sentence.endsWith("fun"))

These methods help in searching text.


Splitting Strings

Strings can be split into parts using a delimiter.

val data = "Scala,Java,Python"
val languages = data.split(",")

languages.foreach(println)

The split() method returns an array of strings.


Immutability of Strings

Strings cannot be changed once created. Instead, operations return new strings.

val original = "Scala"
val modified = original + " Programming"

println(original)
println(modified)

The original string remains unchanged.


Why Strings Are Important

Strings are used everywhere:

  • User input and output
  • File handling
  • Logging and messages
  • Web and API responses

📝 Practice Exercises


Exercise 1

Create a string variable and print its length.

Exercise 2

Use string interpolation to print your name and course.

Exercise 3

Split a comma-separated string into multiple values.


✅ Practice Answers


Answer 1

val text = "Dataplexa"
println(text.length)

Answer 2

val name = "Student"
val course = "Scala"

println(s"$name is learning $course")

Answer 3

val items = "A,B,C"
items.split(",").foreach(println)

What’s Next?

In the next lesson, you will learn about Conditionals in Scala, which allow your programs to make decisions.