0
0
KotlinHow-ToBeginner · 3 min read

How to Replace Text in a String in Kotlin Easily

In Kotlin, you can replace parts of a string using the replace() function. It lets you replace a substring or a character with another string or character easily. For example, val newString = oldString.replace("old", "new") replaces all occurrences of "old" with "new".
📐

Syntax

The replace() function in Kotlin has two main forms:

  • replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String - replaces all occurrences of oldValue with newValue.
  • replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String - replaces all occurrences of a character.

The function returns a new string with replacements; the original string stays unchanged because strings are immutable.

kotlin
val original = "hello world"
val replaced = original.replace("world", "Kotlin")
println(replaced)
Output
hello Kotlin
💻

Example

This example shows how to replace a word in a string and how to replace characters, including ignoring case sensitivity.

kotlin
fun main() {
    val text = "Hello World! Hello Kotlin!"

    // Replace substring
    val replacedText = text.replace("Hello", "Hi")
    println(replacedText)  // Replaces all "Hello" with "Hi"

    // Replace substring ignoring case
    val replacedIgnoreCase = text.replace("hello", "Hey", ignoreCase = true)
    println(replacedIgnoreCase)  // Replaces "Hello" ignoring case

    // Replace character
    val replacedChar = text.replace('!', '.')
    println(replacedChar)  // Replaces '!' with '.'
}
Output
Hi World! Hi Kotlin! Hey World! Hey Kotlin! Hello World. Hello Kotlin.
⚠️

Common Pitfalls

One common mistake is expecting replace() to change the original string. Strings in Kotlin are immutable, so replace() returns a new string instead.

Another pitfall is forgetting to set ignoreCase = true when you want case-insensitive replacement.

kotlin
fun main() {
    val text = "Hello World"

    // Wrong: expecting original string to change
    text.replace("World", "Kotlin")
    println(text)  // Still prints "Hello World"

    // Right: assign the result
    val newText = text.replace("World", "Kotlin")
    println(newText)  // Prints "Hello Kotlin"

    // Case sensitive replacement fails
    val failReplace = text.replace("world", "Kotlin")
    println(failReplace)  // Prints "Hello World"

    // Correct with ignoreCase
    val successReplace = text.replace("world", "Kotlin", ignoreCase = true)
    println(successReplace)  // Prints "Hello Kotlin"
}
Output
Hello World Hello Kotlin Hello World Hello Kotlin
📊

Quick Reference

FunctionDescriptionExample
replace(oldValue: String, newValue: String)Replaces all occurrences of a substring"text.replace(\"old\", \"new\")"
replace(oldValue: String, newValue: String, ignoreCase: Boolean)Replaces substring ignoring case if true"text.replace(\"old\", \"new\", ignoreCase = true)"
replace(oldChar: Char, newChar: Char)Replaces all occurrences of a character"text.replace('a', 'b')"

Key Takeaways

Use the replace() function to create a new string with replacements; original strings do not change.
You can replace substrings or characters with replace() in Kotlin.
Set ignoreCase = true to replace text without case sensitivity.
Always assign the result of replace() to a variable to keep the changed string.
replace() replaces all occurrences, not just the first one.