Beginner 8 min readKotlin 2.0

Kotlin Strings — Declaration, Access, and Common Operations

A String in Kotlin is an immutable sequence of characters. This tutorial covers everything from declaration to common string functions.

What You Will Learn

  • Declare and access Kotlin strings
  • Get length and individual characters
  • Concatenate strings
  • Common string functions: uppercase, lowercase, trim, replace
  • Check if a string is empty or blank

Declaring Strings

Strings are declared with double quotes. Kotlin strings are immutable — once created, their content cannot be changed.

Basic String Declaration

kotlin
fun main() {
    val language = "Kotlin"
    val greeting = "Hello, World!"
    println(language)
    println(greeting)
    println(language.length)
}
Output
Kotlin Hello, World! 6

.length gives the number of characters in the string. "Kotlin" has 6 characters.

Beginner Tip: Strings use double quotes ". Single quotes ' are for Char (a single character). "A" is a String; 'A' is a Char.

Accessing Characters

Access individual characters by index (0-based). The first character is at index 0.

Character Access

kotlin
fun main() {
    val word = "Kotlin"
    println(word[0])   // first character
    println(word[5])   // last character
    println(word.first())
    println(word.last())
}
Output
K n K n

word[0] accesses the character at index 0. .first() and .last() are convenience functions for the first and last characters.

Common Mistake: Accessing an index that is out of bounds (e.g., word[10] on a 6-character string) throws StringIndexOutOfBoundsException.

Common String Functions

Kotlin's String class has many useful built-in functions.

Common String Operations

kotlin
fun main() {
    val text = "  Hello, Kotlin!  "
    println(text.trim())          // remove whitespace
    println(text.trim().uppercase())
    println(text.trim().lowercase())
    println(text.trim().replace("Kotlin", "World"))
    println(text.trim().contains("Hello"))
    println(text.trim().startsWith("Hello"))
    println(text.trim().endsWith("!"))
}
Output
Hello, Kotlin! HELLO, KOTLIN! hello, kotlin! Hello, World! true true true

.trim() removes leading and trailing whitespace. .uppercase() / .lowercase() change case. .replace() substitutes substrings. .contains(), .startsWith(), .endsWith() return booleans.

Checking Empty and Blank Strings

Kotlin provides helpful functions to check whether a string has content.

isEmpty vs isBlank

kotlin
fun main() {
    val empty = ""
    val blank = "   "
    val filled = "Kotlin"

    println(empty.isEmpty())   // true
    println(blank.isEmpty())   // false
    println(blank.isBlank())   // true
    println(filled.isBlank())  // false
}
Output
true false true false

isEmpty() returns true only for a string with zero characters. isBlank() returns true for an empty string or one containing only whitespace.

Best Practice: Use isBlank() instead of isEmpty() for user input validation. A string with only spaces is effectively empty for most purposes.

String Concatenation

Combine strings with + or use string templates (preferred).

Concatenation vs Templates

kotlin
fun main() {
    val first = "Kotlin"
    val second = "Guide"

    // Concatenation with +
    println(first + second)

    // String template (preferred)
    println("$first$second")
    println("Welcome to $first$second.com")
}
Output
KotlinGuide KotlinGuide Welcome to KotlinGuide.com

+ concatenates strings. String templates are more readable and preferred in Kotlin style.

Practice Exercise

Exercisepredict output

What does this print? val s = " Kotlin " println(s.trim().length)

Quick Quiz

Quick Quiz

What is the difference between isEmpty() and isBlank()?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review