.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.

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