replace

inline fun CharSequence.replace(regex: Regex, replacement: String): String(source)

Replaces all occurrences of the given regular expression regex in this char sequence with the specified replacement expression.

This is a convenience function that is equivalent to regex.replace(this, replacement). For details about its behaviour and the substitution syntax of replacement expression, refer to Regex.replace.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"

// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(text.replace(dateRegex, replacement)) // The events are on 2024-09-15 and 2024-10-16.

// One can also reference the matched groups by index
println(text.replace(dateRegex, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.

// Using a backslash to include the special character '$' as a literal in the result
println(text.replace(dateRegex, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16. 
   //sampleEnd
}

inline fun CharSequence.replace(regex: Regex, noinline transform: (MatchResult) -> CharSequence): String(source)

Returns a new string obtained by replacing each substring of this char sequence that matches the given regular expression with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

Since Kotlin

1.0

expect fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String(source)

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}

expect fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String(source)

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}
actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String(source)

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin

1.1

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}

actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String(source)

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin

1.1

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}
actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String(source)

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}

actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String(source)

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}
actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String(source)

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin

1.3

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}

actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String(source)

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin

1.3

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString0 = "Mississippi"
val inputString1 = "Insufficient data for meaningful answer."

println(inputString0.replace('s', 'z')) // Mizzizzippi
println(inputString1.replace("data", "information")) // Insufficient information for meaningful answer. 
   //sampleEnd
}
actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String(source)

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin

1.8

actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String(source)

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin

1.8
actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String(source)

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin

1.8

actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String(source)

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin

1.8