Kotlin String Operations

Kotlin provides a concise set of operations for creating, inspecting, comparing, searching, splitting, and transforming strings. A Kotlin String represents a sequence of characters, and strings are immutable: an operation such as replace(), uppercase(), or trim() returns a new string instead of changing the original value.

This page gives a practical overview of the most commonly used Kotlin string operations and then links to focused tutorials for individual tasks such as string length, interpolation, split, replace, substring, reverse, and character access.

Create and Read Kotlin Strings

Use double quotes to create a regular string. You can read its length with the length property and access a character by its zero-based index.

</>
Copy
fun main() {
    val text = "Kotlin"

    println(text)
    println(text.length)
    println(text[0])
}

The first character is at index 0. Accessing an index outside the valid range throws an exception, so check the string length when an index may come from user input or another variable.

Kotlin
6
K

Kotlin String Templates and Interpolation

Kotlin string templates let you place variable values and expressions directly inside a string. Use $variable for a simple variable and ${expression} for an expression.

</>
Copy
val message = "Hello, $name"
val details = "Name length: ${name.length}"
</>
Copy
fun main() {
    val name = "Mira"
    val score = 92

    println("$name scored $score")
    println("Next score target: ${score + 1}")
}
Mira scored 92
Next score target: 93

Compare and Search Kotlin Strings

Use == for content equality. Common search operations include contains(), startsWith(), endsWith(), and indexOf(). Several string comparison and search functions also support case-insensitive matching through an ignoreCase argument.

</>
Copy
fun main() {
    val text = "Kotlin String Operations"

    println(text.contains("String"))
    println(text.startsWith("Kotlin"))
    println(text.endsWith("Operations"))
    println(text.indexOf("String"))
    println("KOTLIN".equals("kotlin", ignoreCase = true))
}
true
true
true
7
true

Extract, Split, and Join Kotlin Strings

Use substring() when you need part of a string. Use split() to break text into pieces around a delimiter. When you already have a collection of strings, joinToString() combines them with a separator.

</>
Copy
fun main() {
    val filename = "report-2026.csv"
    val parts = filename.split("-")

    println(filename.substring(0, 6))
    println(parts)
    println(listOf("red", "green", "blue").joinToString(" | "))
}
report
[report, 2026.csv]
red | green | blue

Replace, Trim, Change Case, and Reverse Kotlin Strings

Kotlin includes direct operations for common text cleanup and transformation tasks. These operations return new strings, so assign the result when you need to keep the transformed value.

</>
Copy
fun main() {
    val text = "  Kotlin Strings  "

    println(text.trim())
    println(text.trim().uppercase())
    println(text.trim().lowercase())
    println(text.replace("Strings", "Text"))
    println("Kotlin".reversed())
}
Kotlin Strings
KOTLIN STRINGS
kotlin strings
  Kotlin Text  
niltoK

Kotlin Multiline Strings and Raw String Literals

Triple quotes create a raw multiline string. Raw strings can span lines and do not use backslash escaping in the same way as regular quoted strings. trimIndent() is commonly used to remove indentation added only to keep source code readable.

</>
Copy
fun main() {
    val message = """
        Name: Mira
        Language: Kotlin
        Topic: Strings
    """.trimIndent()

    println(message)
}
Name: Mira
Language: Kotlin
Topic: Strings

Check Empty, Blank, and Nullable Kotlin Strings

isEmpty() checks whether a string has zero characters, while isBlank() also treats a string containing only whitespace as blank. For nullable strings, Kotlin provides isNullOrEmpty() and isNullOrBlank().

</>
Copy
fun main() {
    val empty = ""
    val spaces = "   "
    val value: String? = null

    println(empty.isEmpty())
    println(spaces.isBlank())
    println(value.isNullOrEmpty())
    println(value.isNullOrBlank())
}
true
true
true
true

Build and Format Kotlin Strings

String templates are the usual choice for inserting values into text. When you need to assemble many pieces conditionally, buildString() provides a mutable builder internally and returns the completed string. On Kotlin/JVM, String.format() is also available when Java-style format specifiers are specifically required.

</>
Copy
fun main() {
    val name = "Mira"
    val topics = listOf("split", "replace", "reverse")

    val summary = buildString {
        append("Student: $name")
        append("\nTopics: ")
        append(topics.joinToString(", "))
    }

    println(summary)
}
Student: Mira
Topics: split, replace, reverse

Convert Kotlin Strings to Numbers Safely

When a string contains a numeric value, functions such as toInt(), toLong(), and toDouble() parse it into a number. If input may be invalid, the corresponding OrNull form, such as toIntOrNull(), returns null instead of throwing a parsing exception.

</>
Copy
fun main() {
    val valid = "42"
    val invalid = "forty-two"

    println(valid.toInt())
    println(invalid.toIntOrNull())
}
42
null

Kotlin String Operation Tutorials by Task

Kotlin String Basics, Length, Concatenation, and Comparison

Kotlin String Checks with equals, contains, startsWith, and endsWith

Find Text, Count Words, and Extract Kotlin Substrings

Kotlin String Transformations with replace, trim, case conversion, and reverse

Split Kotlin Strings by Delimiters, Lines, and Whitespace

Convert Kotlin Strings to Character, Byte, List, and Set Values

Access, Iterate, Insert, Remove, and Replace Kotlin String Characters

Format Kotlin Strings with Variables and String Templates

Work with Lists, Sets, and Arrays of Kotlin Strings

Choosing the Right Kotlin String Operation

  • Use length, indexing, first(), or last() when reading characters and string size.
  • Use == or equals(..., ignoreCase = true) when comparing text.
  • Use contains(), startsWith(), endsWith(), or indexOf() when searching.
  • Use substring() when extracting a known range of characters.
  • Use split() when parsing delimited text and joinToString() when combining collection elements.
  • Use replace(), trim(), uppercase(), lowercase(), and reversed() for text transformations.
  • Use string templates when inserting variables or expressions into output text.

Kotlin String Operations Summary

Kotlin strings support direct operations for length, character access, comparison, searching, substring extraction, splitting, joining, replacement, trimming, case conversion, reversal, and interpolation. Because strings are immutable, transformation functions return new values instead of modifying the original string. The task-specific tutorials above cover each operation in more detail.

Continue with the Kotlin Tutorial for related Kotlin language topics.