To get the last N characters of a given string in Swift, you can use the `suffix` method or string slicing with negative indices.
In this example,
str1
with a string value of "Hello World"
.suffix
method with String(str1.suffix(5))
to get the last 5 characters.let str1 = "Hello World"
let last5Chars = String(str1.suffix(5))
print("Last 5 characters of the string are: \(last5Chars)")
Last 5 characters of the string are: World
In this example,
str2
with a string value of "apple"
.String(str2.suffix(3))
to get the last 3 characters.let str2 = "apple"
let last3Chars = String(str2.suffix(3))
print("Last 3 characters of the string are: \(last3Chars)")
Last 3 characters of the string are: ple
In this tutorial, we learned How to get Last N Characters in String in Swift language with well detailed examples.