.removePrefix()
Published Oct 27, 2023Updated Nov 1, 2023
Contribute to Docs
The .removePrefix()
method in Kotlin is used to remove a specified prefix from a string if it is present. It returns a new string with the specified prefix removed if it exists. If the original string does not start with the specified prefix, the method returns the original string unchanged.
Syntax
String.removePrefix(prefix)
String
: The original string from which the prefix will be removed.prefix
: The prefix which will be removed from the original string.
Example
This example shows how to use the .removePrefix()
method to remove a prefix from a Kotlin string:
fun main() {val originalString = "HelloWorld.jpg"val prefix = "Hello"val modifiedString = originalString.removePrefix(prefix)println("Modified String: $modifiedString")}
The result will be printed as:
Modified String: World.jpg
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