.removeRange()
Anonymous contributor
Published Oct 28, 2023
Contribute to Docs
The removeRange()
method in Kotlin is used to remove a specified range of characters from a string. It returns a new string with the specified range of characters removed.
Syntax
val modifiedString = originalString.removeRange(startIndex, endIndex)
originalString
: The original string from which you want to remove characters.
startIndex
: The index of the first character to be removed (inclusive).
endIndex
: The index of the character following the last character to be removed (exclusive).
Example
This example shows how to use the removeRange()
method to remove a range of characters from a Kotlin string:
fun main() {val originalString = "Hello, World!"val startIndex = 5val endIndex = 12val modifiedString = originalString.removeRange(startIndex, endIndex)println("Modified String: $modifiedString")}
In this example, removeRange()
method is used to remove characters between indices 5 (inclusive) and 12 (exclusive). The result will be printed as:
Modified String: Hello!
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours