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 = "Hello 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 = "Hello 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 = "Hello Polo" let lastThree = name.suffix(3)! print(lastThree)