Remove Last Two Characters in a String
Solution 1
update: Xcode 9 • Swift 4 or later
String now conforms to RangeReplaceableCollection so you can use collection method dropLast straight in the String and therefore an extension it is not necessary anymore. The only difference is that it returns a Substring. If you need a String you need to initialize a new one from it:
let string = "0123456789"
let substring1 = string.dropLast(2) // "01234567"
let substring2 = substring1.dropLast() // "0123456"
let result = String(substring2.dropLast()) // "012345"
We can also extend LosslessStringConvertible
to add trailing syntax which I think improves readability:
extension LosslessStringConvertible {
var string: String { .init(self) }
}
Usage:
let result = substring.dropLast().string
Solution 2
var name: String = "Dolphin"
let endIndex = name.index(name.endIndex, offsetBy: -2)
let truncated = name.substring(to: endIndex)
print(name) // "Dolphin"
print(truncated) // "Dolph"
Solution 3
swift 4:
let str = "Hello, playground"
let newSTR1 = str.dropLast(3)
print(newSTR1)
output: "Hello, playgro"
//---------------//
let str = "Hello, playground"
let newSTR2 = str.dropFirst(2)
print(newSTR2)
output: "llo, playground"
Solution 4
Use removeSubrange(Range<String.Index>)
just like:
var str = "Hello, playground"
str.removeSubrange(Range(uncheckedBounds: (lower: str.index(str.endIndex, offsetBy: -2), upper: str.endIndex)))
This will crash if the string is less than 2 characters long. Is that a requirement for you?
Solution 5
Better to use removeLast()
var myString = "Hello world"
myString.removeLast(2)
output : "Hello wor"
Ben
Building my coding skills so I can help make the world a better place
Updated on March 27, 2020Comments
-
Ben about 4 years
Is there a quick way to remove the last two characters in a String in Swift? I see there is a simple way to remove the last character as clearly noted here. Do you know how to remove the last two characters? Thanks!
-
Rajan Maheshwari over 7 yearsYou just copy pasted from the link given by OP
-
Naveen Ramanathan over 7 yearsThe original link removes the last character, not the last two
-
dfrib over 7 yearsIn contrast to the
dropLast(..)
method shown in @LeoDabus answer, the method in this answer will yield a runtime exception for strings that are less than two characters (empty of only one character). Also, whereas thedropLast
method is Swift native,.substring
is bridged via Foundation fromNSString
(not entirely Swift native). -
aremvee about 7 yearswouldn't better question be, how to select the first N characters, otherwise you need to test you'd have enough to stay non-nil
-
ssowri1 over 6 yearsThank you! It's saved my time.
-
Malfunction over 6 yearsI just love swift.
-
Uncommon almost 6 yearsNote that
String.dropLast()
returns aSubstring
so you may need to convert it to a String depending on how you're using it. -
Leo Dabus almost 6 years@Uncommon if you read the whole answer you would see that it is already stated in the post
-
koen almost 5 yearsThis will fail if the string is shorter than 2. Per the docs: "Attempting to remove more elements than exist in the collection triggers a runtime error."