Kotlin .removePrefix()

itutu_98's avatar
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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours

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

All contributors

Contribute to Docs

Learn Kotlin on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours