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.
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.
val message = "Hello, $name"
val details = "Name length: ${name.length}"
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.
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.
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.
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.
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().
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.
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.
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 – Initialize string
- Kotlin – Create empty string
- Kotlin – Define a string constant
- Kotlin – Get string length
- Kotlin – Print a string
- Kotlin – Concatenate strings
- Kotlin – Compare strings
- Kotlin – Multiline strings
Kotlin String Checks with equals, contains, startsWith, and endsWith
- Kotlin – Check if strings are equal
- Kotlin – Check if strings are equal ignoring case
- Kotlin – Check if string is empty
- Kotlin – Check if string contains specific substring
- Kotlin – Check if string contains specific character
- Kotlin – Check if string starts with specific prefix
- Kotlin – Check if string starts with specific character
- Kotlin – Check if string ends with specific suffix
- Kotlin – Check if string ends with specific character
- Kotlin – Check is string matches regular expression
Find Text, Count Words, and Extract Kotlin Substrings
- Kotlin – Count number of words in string
- Kotlin – Find index of substring
- Kotlin – Get random character from string
- Kotlin – Substring
Kotlin String Transformations with replace, trim, case conversion, and reverse
- Kotlin – Capitalize a string
- Kotlin – Convert string to lowercase
- Kotlin – Convert string to uppercase
- Kotlin – Filter characters of string
- Kotlin – Join strings by a separator
- Kotlin – Remove first N characters from string
- Kotlin – Remove last N haracters from string
- Kotlin – Repeat string N times
- Kotlin – Reverse a string
- Kotlin – Sort characters in string
- Kotlin – String replace
- Kotlin – Trim white spaces around string
Split Kotlin Strings by Delimiters, Lines, and Whitespace
- Kotlin – Split string
- Kotlin – Split string to lines
- Kotlin – Split string by comma
- Kotlin – Split string by single space
- Kotlin – Split string by any whitespace character
- Kotlin – Split string by one or more spaces
Convert Kotlin Strings to Character, Byte, List, and Set Values
- Kotlin – Convert char array to string
- Kotlin – Convert string to char array
- Kotlin – Convert byte array to string
- Kotlin – Convert string to byte array
- Kotlin – Convert string to list of characters
- Kotlin – Convert list of characters to string
- Kotlin – Convert string to set of characters
Access, Iterate, Insert, Remove, and Replace Kotlin String Characters
- Kotlin – Get character at specific index in string
- Kotlin – Get first character in string
- Kotlin – Get last character in string
- Kotlin – Iterate over each character in string
- Kotlin – Insert character at specific index in string
- Kotlin – Get unique characters in string
- Kotlin – Remove character at specific index in string
- Kotlin – Remove first character in string
- Kotlin – Remove last character in string
- Kotlin – Replace specific character with another in a string
- Kotlin – Remove specific character in a string
Format Kotlin Strings with Variables and String Templates
Work with Lists, Sets, and Arrays of Kotlin Strings
- Kotlin – List of strings
- Kotlin String List – Filter based on Length
- Kotlin – Filter only strings in this list
- Kotlin List – Filter non-empty strings
- Kotlin – Create a set of strings
- Kotlin – Create string array
- Kotlin – Sort array of strings
- Kotlin – Sort string array based on string length
Choosing the Right Kotlin String Operation
- Use
length, indexing,first(), orlast()when reading characters and string size. - Use
==orequals(..., ignoreCase = true)when comparing text. - Use
contains(),startsWith(),endsWith(), orindexOf()when searching. - Use
substring()when extracting a known range of characters. - Use
split()when parsing delimited text andjoinToString()when combining collection elements. - Use
replace(),trim(),uppercase(),lowercase(), andreversed()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.
TutorialKart.com