.removeSuffix()
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
- 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