How to get the last character from a string in Swift
In this post we will give you information about How to get the last character from a string in Swift. Hear we will give you detail about How to get the last character from a string in SwiftAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to get the last character from a string in Swift.
Getting the last character
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)
Output:
"o"
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)
The 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)
Output:
"olo"
Related
Hope this code and post will helped you for implement How to get the last character from a string in Swift. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs