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

Strings

String concatenation, template expressions, length, and commonly used string functions.

Simple enough for a 5-year-old to understand

Template expressions

The dollar sign is used to embed variables into text, making concatenation very readable.

Example code
1
val name = "Alex"
2
val age = 36
3
println("$name $age years old")
Alex is a 36-year-old writer.
Output / InputAlex is a 36-year-old writer.
Output / Input
💡 Hint: The plus sign can also be used for concatenation, but templates are more readable.

🎯 Exercise & Practice

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

1.What does a variable name written with a dollar sign inside the text do?

About this topic

In Kotlin, texts are of String type and are immutable: when you convert a text to uppercase, a new text is generated, and the old one remains as it is. All operations on text work with this rule.

String templates are one of Kotlin's most used conveniences. You include the value in the text by putting a dollar sign before the variable name; if you want to write an expression, you use curly braces. This is both more readable than joining with plus and generates fewer intermediate objects.

An important difference for those coming from Java: In Kotlin, the equality operator compares the contents of texts. In Java, this operator checks object identity and is a classic source of bugs in text comparison; Kotlin has fixed this.

What you will learn

  • Creating and concatenating strings
  • String templates and writing expressions
  • Length, search, and conversion operations
  • Content comparison
  • Writing multi-line strings

Who is this for?

  • Those who will display text in the interface
  • Developers coming from Java who are used to equality comparison
  • Those who need text formatting

When to use?

Every label shown to the user, every saved name, and every field going to the server is text; text operations make up a significant portion of application code.

For long constant texts, SQL queries, and JSON templates, multiline texts eliminate the clutter of escape characters.

Common mistakes

Wrong

val a = "hello"
val b = "hel" + "lo"
println(a === b)

The triple equals sign compares object identity, not content; this comparison can yield incorrect results depending on the situation. For content comparison, double equals is sufficient and works as you expect in Kotlin.

Correct

val a = "hello"
val b = "hel" + "lo"
println(a == b)

Wrong

val items = listOf("a", "b")
println("Count: $items.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 items = listOf("a", "b")
println("Count: ${items.size}")

Wrong

val price = 100
println("Price: $price$")

The trailing dollar sign is considered the start of a template and the compiler looks for an expression following it; you get an "Expecting an expression" error. To type an actual dollar sign inside the text, you need to use an escape.

Correct

val price = 100
println("Price: $price\$")

Frequently asked questions

How to write multi-line strings?

Triple double quotes are used and escape characters are not needed inside. If you want to preserve indentation in the source code, the trimIndent method clears the common leading spaces and leaves the text in the alignment you expect.

Should I use plus or a template for string concatenation?

Templates are preferred: they are more readable and the compiler turns them into efficient concatenation. If you are adding many parts inside a loop, using StringBuilder is still the fastest way.

Why does text look like it's changing even though it is immutable?

Because method calls return a new text and you usually assign the result to the same variable. The object pointed to by the variable changes, the text itself does not change. If you don't assign the result anywhere, the operation appears to do nothing.

Summary

Texts are immutable, operations return a new text.

Double equals performs content comparison.

Curly braces are required to write expressions in a template.

The actual dollar sign is written with an escape.

Multi-line texts are written with three pairs of double quotes.

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