To check if a string ends with a specific suffix in Swift, you can use the hasSuffix()
method of the String class.
In this example,
str1
with a string value of "Hello World"
.hasSuffix()
method of the string object str1
with the suffix "World"
.hasSuffix()
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.let str1 = "Hello World"
let endsWithWorld = str1.hasSuffix("World")
print("String ends with suffix 'World': \(endsWithWorld)")
String ends with suffix 'World': true
In this example,
str2
with a string value of "apple"
.hasSuffix()
method of the string object str2
with the suffix "banana"
.hasSuffix()
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.let str2 = "apple"
let endsWithBanana = str2.hasSuffix("banana")
print("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 Swift language with well detailed examples.