Spec-Zone.ru › Kotlin 2

replaceRange

kotlin-stdlib/kotlin.text/replaceRange

В Kotlin начиная с версии: 1.0

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

Возвращает последовательность символов, содержащую текст этой последовательности, в которой часть в указанном диапазоне заменена последовательностью символов replacement.

Параметры

startIndex

индекс первого символа, который нужно заменить.

endIndex

индекс первого символа после заменяемой части, которую нужно сохранить в строке.

Примеры

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

Заменяет часть строки в указанном диапазоне последовательностью символов replacement.

Параметры

startIndex

индекс первого символа, который нужно заменить.

endIndex

индекс первого символа после заменяемой части, которую нужно сохранить в строке.

Примеры

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
}

fun CharSequence.replaceRange(range: IntRange, replacement: CharSequence): CharSequence

Возвращает последовательность символов, содержащую текст этой последовательности, в которой часть в указанном диапазоне заменена последовательностью символов replacement.

Конечный индекс диапазона включается в заменяемую часть.

Примеры

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

Заменяет часть строки в указанном диапазоне строкой replacement.

Конечный индекс диапазона включается в заменяемую часть.

Примеры

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
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.text/replace-range.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API