replaceRange

fun CharSequence.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): CharSequence(source)

Returns a char sequence with content of this char sequence where its part at the given range is replaced with the replacement char sequence.

Since Kotlin

1.0

Parameters

startIndex

the index of the first character to be replaced.

endIndex

the index of the first character after the replacement to keep in the string.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val text = "Hello, world!"

// Replacing the range that corresponds to the "world" substring with "Kotlin"
println(text.replaceRange(startIndex = 7, endIndex = 12, replacement = "Kotlin")) // Hello, Kotlin!

// Also possible to replace using a Range
println(text.replaceRange(7..11, replacement = "Kotlin")) // Hello, Kotlin!

// Empty replacement effectively removes the range
println(text.replaceRange(7..11, "")) // Hello, !

// Replacing at the end of the string: endIndex is exclusive
println(text.replaceRange(0, 5, "Hey")) // Hey, world!

// But for the overload accepting the range, range's end is inclusive (thus the comma is also replaced)
println(text.replaceRange(0..5, "Hey")) // Hey world!

// Throws if startIndex is greater than endIndex
// text.replaceRange(startIndex = 7, endIndex = 4, replacement = "Kotlin") // will fail
// Throws if startIndex or endIndex is out of the string indices range
// text.replaceRange(startIndex = 7, endIndex = 20, replacement = "Kotlin") // will fail
// For ranges, the end is inclusive, so it has to be lower than the length of a char sequence
// text.replaceRange(7..text.length, replacement = "Kotlin") // will fail 
   //sampleEnd
}

inline fun String.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): String(source)

Replaces the part of the string at the given range with the replacement char sequence.

Since Kotlin

1.0

Parameters

startIndex

the index of the first character to be replaced.

endIndex

the index of the first character after the replacement to keep in the string.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val text = "Hello, world!"

// Replacing the range that corresponds to the "world" substring with "Kotlin"
println(text.replaceRange(startIndex = 7, endIndex = 12, replacement = "Kotlin")) // Hello, Kotlin!

// Also possible to replace using a Range
println(text.replaceRange(7..11, replacement = "Kotlin")) // Hello, Kotlin!

// Empty replacement effectively removes the range
println(text.replaceRange(7..11, "")) // Hello, !

// Replacing at the end of the string: endIndex is exclusive
println(text.replaceRange(0, 5, "Hey")) // Hey, world!

// But for the overload accepting the range, range's end is inclusive (thus the comma is also replaced)
println(text.replaceRange(0..5, "Hey")) // Hey world!

// Throws if startIndex is greater than endIndex
// text.replaceRange(startIndex = 7, endIndex = 4, replacement = "Kotlin") // will fail
// Throws if startIndex or endIndex is out of the string indices range
// text.replaceRange(startIndex = 7, endIndex = 20, replacement = "Kotlin") // will fail
// For ranges, the end is inclusive, so it has to be lower than the length of a char sequence
// text.replaceRange(7..text.length, replacement = "Kotlin") // will fail 
   //sampleEnd
}

Returns a char sequence with content of this char sequence where its part at the given range is replaced with the replacement char sequence.

The end index of the range is included in the part to be replaced.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val text = "Hello, world!"

// Replacing the range that corresponds to the "world" substring with "Kotlin"
println(text.replaceRange(startIndex = 7, endIndex = 12, replacement = "Kotlin")) // Hello, Kotlin!

// Also possible to replace using a Range
println(text.replaceRange(7..11, replacement = "Kotlin")) // Hello, Kotlin!

// Empty replacement effectively removes the range
println(text.replaceRange(7..11, "")) // Hello, !

// Replacing at the end of the string: endIndex is exclusive
println(text.replaceRange(0, 5, "Hey")) // Hey, world!

// But for the overload accepting the range, range's end is inclusive (thus the comma is also replaced)
println(text.replaceRange(0..5, "Hey")) // Hey world!

// Throws if startIndex is greater than endIndex
// text.replaceRange(startIndex = 7, endIndex = 4, replacement = "Kotlin") // will fail
// Throws if startIndex or endIndex is out of the string indices range
// text.replaceRange(startIndex = 7, endIndex = 20, replacement = "Kotlin") // will fail
// For ranges, the end is inclusive, so it has to be lower than the length of a char sequence
// text.replaceRange(7..text.length, replacement = "Kotlin") // will fail 
   //sampleEnd
}

inline fun String.replaceRange(range: IntRange, replacement: CharSequence): String(source)

Replace the part of string at the given range with the replacement string.

The end index of the range is included in the part to be replaced.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val text = "Hello, world!"

// Replacing the range that corresponds to the "world" substring with "Kotlin"
println(text.replaceRange(startIndex = 7, endIndex = 12, replacement = "Kotlin")) // Hello, Kotlin!

// Also possible to replace using a Range
println(text.replaceRange(7..11, replacement = "Kotlin")) // Hello, Kotlin!

// Empty replacement effectively removes the range
println(text.replaceRange(7..11, "")) // Hello, !

// Replacing at the end of the string: endIndex is exclusive
println(text.replaceRange(0, 5, "Hey")) // Hey, world!

// But for the overload accepting the range, range's end is inclusive (thus the comma is also replaced)
println(text.replaceRange(0..5, "Hey")) // Hey world!

// Throws if startIndex is greater than endIndex
// text.replaceRange(startIndex = 7, endIndex = 4, replacement = "Kotlin") // will fail
// Throws if startIndex or endIndex is out of the string indices range
// text.replaceRange(startIndex = 7, endIndex = 20, replacement = "Kotlin") // will fail
// For ranges, the end is inclusive, so it has to be lower than the length of a char sequence
// text.replaceRange(7..text.length, replacement = "Kotlin") // will fail 
   //sampleEnd
}