removeRange

fun CharSequence.removeRange(startIndex: Int, endIndex: Int): CharSequence(source)

Returns a CharSequence obtained by removing the specified subsequence from this char sequence.

Since Kotlin

1.0

Parameters

startIndex

the beginning (inclusive) of the subsequence to remove.

endIndex

the end (exclusive) of the subsequence to remove.

Throws

or IllegalArgumentException when startIndex or endIndex is out of range of this char sequence indices or when startIndex > endIndex.

Samples

import java.util.Locale
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(source)

Returns a String obtained by removing the specified substring from this string.

Since Kotlin

1.0

Parameters

startIndex

the beginning (inclusive) of the substring to remove.

endIndex

the end (exclusive) of the substring to remove.

Throws

or IllegalArgumentException when startIndex or endIndex is out of range of this string indices or when startIndex > endIndex.

Samples

import java.util.Locale
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
}

Returns a CharSequence obtained by removing the specified subsequence from this char sequence.

Since Kotlin

1.0

Parameters

range

the range of indexes of the subsequence to remove. Note: the character at index IntRange.endInclusive of the range is removed as well.

Samples

import java.util.Locale
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(source)

Returns a String obtained by removing the specified substring from this char sequence.

Since Kotlin

1.0

Parameters

range

the range of indexes of the substring to remove. Note: the character at index IntRange.endInclusive of the range is removed as well.

Samples

import java.util.Locale
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
}