Skip to main content

String Basics in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

String Basics is a fundamental concept every Kotlin developer should understand. Strings appear in almost every program — user input, file paths, API responses, and UI labels. Kotlin’s standard library provides concise helpers for common text tasks.

String is an immutable sequence of characters in Kotlin. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • String is an immutable sequence of characters in Kotlin.
  • Strings are declared using double quotes or triple quotes.
  • String is a reference type and is implemented as a class.

Syntax

var name: String = "Kotlin"

String Basics in Kotlin Example Program in Kotlin

fun main(args: Array<String>) {
    var language = "Kotlin"
    println("Language: $language")
    println("First character: ${language[0]}")
}

Sample Output

Language: Kotlin
First character: K

When to use

Use string functions when formatting output, validating user input, splitting CSV data, or building URLs and file paths.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. var language = "Kotlin" assigns or updates a value used later in the program.

  3. The println("Language: $language") statement writes a line to the console — this produces part of the sample output below.

  4. The println("First character: ${language[0]}") statement writes a line to the console — this produces part of the sample output below.

  5. String is an immutable sequence of characters in Kotlin.

  6. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Prefer string templates ("Hello, $name") over concatenation for readability.
  • Use trim(), isBlank(), and isEmpty() to validate user input consistently.
  • Choose StringBuilder when building large strings inside loops.

Common Mistakes

  • Calling substring with wrong end indices — prefer substringAfter / substringBefore helpers.
  • Using == when case-insensitive comparison is needed — use equals(other, ignoreCase = true).
  • Concatenating inside tight loops instead of using StringBuilder.

Key Points

  • String is an immutable sequence of characters in Kotlin.
  • Strings are declared using double quotes or triple quotes.
  • String is a reference type and is implemented as a class.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is String Basics in Kotlin?
String is an immutable sequence of characters in Kotlin.
When should I use String Basics?
Use string functions when formatting output, validating user input, splitting CSV data, or building URLs and file paths.
How is String Basics different from Java?
String is a reference type and is implemented as a class.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials