To remove the last N characters of a string in Swift, you can use the prefix method with the range from 0 to string.count - N.
In this example,
str1
with a value of "Hello World"
.let str1 = "Hello World"
let modifiedStr = String(str1.prefix(str1.count - 3))
print("Modified string is:", modifiedStr)
Modified string is: Hello Wo
In this example,
str2
with a value of "apple"
.let str2 = "apple"
let modifiedStr = String(str2.prefix(str2.count - 2))
print("Modified string is:", modifiedStr)
Modified string is: app
In this tutorial, we learned How to Remove last N Characters in a String in Swift language with well detailed examples.