In this tutorial, we are going to learn about how to get the last character from a string in Swift.
To get the last character of a string, we can use the
string.last property in Swift.
Here is an example, that gets the last character
o from a given string:
let name = "Polo" let lastCharacter = name.last! print(lastCharacter)
Similarly, we can also use the
suffix() method by passing
1 as an argument to it.
let name = "Polo" let lastCharacter = name.suffix(1)! print(lastCharacter)
suffix() method can also be used to get the last n characters from a string.
Example for getting last 3 characters:
let name = "Polo" let lastThree = name.suffix(3)! print(lastThree)