To check if a string ends with a specific suffix in Kotlin, you can use the endsWith()
function of the String class.
In this example,
str1
with a value of "Hello World"
.endsWith()
function of the string object str1
with the suffix "World"
.endsWith()
function returns true
if the string ends with the specified suffix; otherwise, it returns false
.fun main(args: Array<String>) {
val str1 = "Hello World"
val endsWithWorld = str1.endsWith("World")
println("String ends with suffix 'World': $endsWithWorld")
}
String ends with suffix 'World': true
In this example,
str2
with a value of "apple"
.endsWith()
function of the string object str2
with the suffix "banana"
.endsWith()
function returns true
if the string ends with the specified suffix; otherwise, it returns false
.fun main(args: Array<String>) {
val str2 = "apple"
val endsWithBanana = str2.endsWith("banana")
println("String ends with suffix 'banana': $endsWithBanana")
}
String ends with suffix 'banana': false
In this tutorial, we learned How to Check if String ends with Specific Suffix in Kotlin language with well detailed examples.