In Swift, you can use the last
property of a string to get the last character.
In this example,
str
with a value of "Hello World"
.last
property with str
to get the last character.let str = "Hello World"
let lastChar = str.last
print("Last character of the string is: \(lastChar)")
Last character of the string is: Optional("d")
In this example,
str
with a value of "apple"
.last
property with str
to get the last character.let str = "apple"
let lastChar = str.last
print("Last character of the string is: \(lastChar)")
Last character of the string is: Optional("e")
In this tutorial, we learned How to get Last Character in String in Swift language with well detailed examples.