Core API – Kotlin Programming Language (original) (raw)
substring
Returns a substring specified by the given range of indices.
Since Kotlin
1.0
Returns a substring of chars from a range of this char sequence starting at the startIndex and ending right before the endIndex.
Since Kotlin
1.0
startIndex
the start index (inclusive).
endIndex
the end index (exclusive). If not specified, the length of the char sequence is used.
Returns a substring of chars at indices from the specified range of this char sequence.
Since Kotlin
1.0
Returns a substring of this string that starts at the specified startIndex and continues to the end of the string.
Since Kotlin
1.0
startIndex
the start index (inclusive).
when startIndex is negative or exceeds the length if the string.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0)) // abcde
println(str.substring(1)) // bcde
// startIndex exceeds the string length
// str.substring(6) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns the substring of this string starting at the startIndex and ending right before the endIndex.
Since Kotlin
1.0
startIndex
the start index (inclusive).
endIndex
the end index (exclusive).
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0, 0)) //
println(str.substring(0, 1)) // a
println(str.substring(5, 5)) //
// endIndex exceeds string length
// str.substring(0, 6) // will fail with IndexOutOfBoundsException
// endIndex is smaller than the startIndex
// str.substring(1, 0) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns a substring of this string that starts at the specified startIndex and continues to the end of the string.
Since Kotlin
1.0
startIndex
the start index (inclusive).
when startIndex is negative or exceeds the length if the string.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0)) // abcde
println(str.substring(1)) // bcde
// startIndex exceeds the string length
// str.substring(6) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns the substring of this string starting at the startIndex and ending right before the endIndex.
Since Kotlin
1.0
startIndex
the start index (inclusive).
endIndex
the end index (exclusive).
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0, 0)) //
println(str.substring(0, 1)) // a
println(str.substring(5, 5)) //
// endIndex exceeds string length
// str.substring(0, 6) // will fail with IndexOutOfBoundsException
// endIndex is smaller than the startIndex
// str.substring(1, 0) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns a substring of this string that starts at the specified startIndex and continues to the end of the string.
Since Kotlin
1.3
startIndex
the start index (inclusive).
when startIndex is negative or exceeds the length if the string.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0)) // abcde
println(str.substring(1)) // bcde
// startIndex exceeds the string length
// str.substring(6) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns the substring of this string starting at the startIndex and ending right before the endIndex.
Since Kotlin
1.3
startIndex
the start index (inclusive).
endIndex
the end index (exclusive).
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0, 0)) //
println(str.substring(0, 1)) // a
println(str.substring(5, 5)) //
// endIndex exceeds string length
// str.substring(0, 6) // will fail with IndexOutOfBoundsException
// endIndex is smaller than the startIndex
// str.substring(1, 0) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns a substring of this string that starts at the specified startIndex and continues to the end of the string.
Since Kotlin
1.8
startIndex
the start index (inclusive).
when startIndex is negative or exceeds the length if the string.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0)) // abcde
println(str.substring(1)) // bcde
// startIndex exceeds the string length
// str.substring(6) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns the substring of this string starting at the startIndex and ending right before the endIndex.
Since Kotlin
1.8
startIndex
the start index (inclusive).
endIndex
the end index (exclusive).
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0, 0)) //
println(str.substring(0, 1)) // a
println(str.substring(5, 5)) //
// endIndex exceeds string length
// str.substring(0, 6) // will fail with IndexOutOfBoundsException
// endIndex is smaller than the startIndex
// str.substring(1, 0) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns a substring of this string that starts at the specified startIndex and continues to the end of the string.
Since Kotlin
1.8
startIndex
the start index (inclusive).
when startIndex is negative or exceeds the length if the string.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0)) // abcde
println(str.substring(1)) // bcde
// startIndex exceeds the string length
// str.substring(6) // will fail with IndexOutOfBoundsException
//sampleEnd
}Returns the substring of this string starting at the startIndex and ending right before the endIndex.
Since Kotlin
1.8
startIndex
the start index (inclusive).
endIndex
the end index (exclusive).
Samples
import kotlin.test.*
fun main() {
//sampleStart
val str = "abcde"
println(str.substring(0, 0)) //
println(str.substring(0, 1)) // a
println(str.substring(5, 5)) //
// endIndex exceeds string length
// str.substring(0, 6) // will fail with IndexOutOfBoundsException
// endIndex is smaller than the startIndex
// str.substring(1, 0) // will fail with IndexOutOfBoundsException
//sampleEnd
}