remove Range
Версия Kotlin: 1.0
fun CharSequence.removeRange(startIndex: Int, endIndex: Int): CharSequence
Возвращает CharSequence, полученную удалением указанной подпоследовательности из этой символьной последовательности.
Параметры
начало (включительно) удаляемой подпоследовательности.
конец (не включительно) удаляемой подпоследовательности.
Исключения
или IllegalArgumentException, если startIndex или endIndex выходит за пределы индексов этой символьной последовательности или если startIndex > endIndex.
Примеры
import kotlin.test.*
fun main() {
//sampleStart
val text = StringBuilder("Hello, world!")
// Removing the range that correspond to the "world" subsequence
println(text.removeRange(startIndex = 7, endIndex = 12)) // Hello, !
// Also possible to remove using a Range
println(text.removeRange(7..11)) // Hello, !
// The original char sequence is not changed
println(text) // Hello, world!
// Throws if startIndex is greater than endIndex
// text.removeRange(startIndex = 7, endIndex = 4) // will fail
// Throws if startIndex or endIndex is out of range of the char sequence indices
// text.removeRange(startIndex = 7, endIndex = 20) // will fail
//sampleEnd
}
inline fun String.removeRange(startIndex: Int, endIndex: Int): String
Возвращает String, полученную удалением указанной подстроки из этой строки.
Параметры
начало (включительно) удаляемой подстроки.
конец (не включительно) удаляемой подстроки.
Исключения
или IllegalArgumentException, если startIndex или endIndex выходит за пределы индексов этой строки или если startIndex > endIndex.
Примеры
import kotlin.test.*
fun main() {
//sampleStart
val text = "Hello, world!"
// Removing the range that correspond to the "world" substring
println(text.removeRange(startIndex = 7, endIndex = 12)) // Hello, !
// Also possible to remove using a Range
println(text.removeRange(7..11)) // Hello, !
// The original string is not changed
println(text) // Hello, world!
// Throws if startIndex is greater than endIndex
// text.removeRange(startIndex = 7, endIndex = 4) // will fail
// Throws if startIndex or endIndex is out of range of the string indices
// text.removeRange(startIndex = 7, endIndex = 20) // will fail
//sampleEnd
}
fun CharSequence.removeRange(range: IntRange): CharSequence
Возвращает CharSequence, полученную удалением указанной подпоследовательности из этой символьной последовательности.
Параметры
диапазон индексов удаляемой подпоследовательности. Примечание: символ с индексом IntRange.endInclusive диапазона range также удаляется.
Примеры
import kotlin.test.*
fun main() {
//sampleStart
val text = StringBuilder("Hello, world!")
// Removing the range that correspond to the "world" subsequence
println(text.removeRange(startIndex = 7, endIndex = 12)) // Hello, !
// Also possible to remove using a Range
println(text.removeRange(7..11)) // Hello, !
// The original char sequence is not changed
println(text) // Hello, world!
// Throws if startIndex is greater than endIndex
// text.removeRange(startIndex = 7, endIndex = 4) // will fail
// Throws if startIndex or endIndex is out of range of the char sequence indices
// text.removeRange(startIndex = 7, endIndex = 20) // will fail
//sampleEnd
}
inline fun String.removeRange(range: IntRange): String
Возвращает String, полученную удалением указанной подстроки из этой символьной последовательности.
Параметры
диапазон индексов удаляемой подстроки. Примечание: символ с индексом IntRange.endInclusive диапазона range также удаляется.
Примеры
import kotlin.test.*
fun main() {
//sampleStart
val text = "Hello, world!"
// Removing the range that correspond to the "world" substring
println(text.removeRange(startIndex = 7, endIndex = 12)) // Hello, !
// Also possible to remove using a Range
println(text.removeRange(7..11)) // Hello, !
// The original string is not changed
println(text) // Hello, world!
// Throws if startIndex is greater than endIndex
// text.removeRange(startIndex = 7, endIndex = 4) // will fail
// Throws if startIndex or endIndex is out of range of the string indices
// text.removeRange(startIndex = 7, endIndex = 20) // 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/remove-range.html