Kotlin .removeSuffix()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 27, 2023
Contribute to Docs

The .removeSuffix() method in Kotlin is used to remove a specified suffix from a string if it is present. The .removeSuffix() method returns a new string with the specified suffix removed if it exists. If the original string does not end with the specified suffix, 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.removeSuffix(suffix)
  • String: The original string from which the suffix will be removed.
  • suffix: The suffix that will be removed from the original string.

Example

This example shows how to use the .removeSuffix() method to remove a suffix from a Kotlin string:

fun main() {
val originalString = "HelloWorld.jpg"
val suffix = ".jpg"
val modifiedString = originalString.removeSuffix(suffix)
println("Modified String: $modifiedString")
}

The result will be printed as:

Modified String: HelloWorld

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