HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ScalaVariables (val / var)Data TypesStringsListsConditions (if / match)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Scala›Strings

Strings

Embedding values in text with s-interpolation, length and string functions.

Simple enough for a 5-year-old to understand

s-interpolation

If you put an s at the beginning of the text, you can embed variables inside it using a dollar sign.

Example code
1
val name = "Alex"
2
val age = 36
3
println(s"$name $age years old")
Alex is a 36-year-old writer.
Output / InputAlex is a 36-year-old writer.
Output / Input
💡 Hint: Without the s at the beginning, the dollar sign is written as is.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which letter is placed at the beginning of the text to embed values into it?

About this topic

Strings in Scala are immutable and identical to Java's string type. When you transform a string, a new string is produced; the original remains unchanged and you must assign the result somewhere.

Scala's string interpolation works with a letter placed before the string. The most common is the format that injects values; there are other formats that do formatting and do not process escape sequences. This design allows you to explicitly choose the interpolation behavior.

An important difference for those coming from Java: in Scala, the equality operator compares the contents of strings. In Java, this operator looks at object identity and is a classic source of bugs in string comparison; Scala has fixed this.

What you will learn

  • Creating and concatenating strings
  • Interpolation formats
  • Content comparison
  • Writing multi-line strings
  • String functions

Who is this for?

  • developers transitioning from Java
  • Those who forget the interpolation letter
  • Those generating report and log output

When to use?

Record formatting and log messages in data processing pipelines are produced using string interpolation.

For long SQL queries and JSON templates, multi-line strings eliminate escape clutter.

Common mistakes

Wrong

val liste = List(1, 2, 3)
println(s"Count: $liste.size")

The dollar sign only takes the variable name; the dot and everything after it are considered plain parts of the text, so the list itself and then ".size" are printed to the screen. If you want to use an expression, curly braces are required.

Correct

val liste = List(1, 2, 3)
println(s"Count: ${liste.size}")

Wrong

val a = "hello"
val b = new String("hello")
println(a eq b)

The identity comparison operator checks whether two references point to the same object, and returns false here. For content comparison, the equality operator should be used; in Scala, this operator behaves as you expect.

Correct

val a = "hello"
val b = new String("hello")
println(a == b)

Wrong

val metin = "hello"
metin.toUpperCase
println(metin)

Since strings are immutable, the method does not modify the original, it returns a new string. Because you didn't assign the result anywhere, the lowercase version is still printed to the screen. Ignoring the return value is a silent error.

Correct

val metin = "hello"
val buyuk = metin.toUpperCase
println(buyuk)

Frequently asked questions

How are multi-line strings aligned?

They are written with three double quotes and appear indented in the source code. When you put a margin character at the beginning of each line and call the strip margin method, the indentation is removed from the output and the text looks neat.

Which interpolation letter should I choose?

The standard one is sufficient for injecting values. If you need decimal place or alignment control, you use the formatting version, and if you want escape sequences to remain as-is, you use the raw version — this is very useful in regular expressions.

Is performance important in string concatenation?

It is negligible for one-off concatenations. If you are appending many pieces inside a loop, a new string is produced at each step; in this case, using a builder or collecting the pieces in a collection and joining them at the end is noticeably faster.

Summary

Strings are immutable.

A letter precedes the string for interpolation.

Writing expressions in interpolation requires curly braces.

The equality operator compares contents.

Multi-line strings are aligned with margin stripping.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code