76

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!

Ben
  • 3,346
  • 6
  • 32
  • 51
  • http://stackoverflow.com/questions/32849789/how-to-get-last-4-characters-of-a-string – LF00 May 08 '17 at 04:41

5 Answers5

165

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
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
60
var name: String = "Dolphin"
let endIndex = name.index(name.endIndex, offsetBy: -2)
let truncated = name.substring(to: endIndex)
print(name)      // "Dolphin"
print(truncated) // "Dolph"
Community
  • 1
  • 1
Naveen Ramanathan
  • 2,166
  • 1
  • 19
  • 21
  • 4
    You just copy pasted from the link given by OP – Rajan Maheshwari Oct 13 '16 at 18:15
  • 3
    The original link removes the last character, not the last two – Naveen Ramanathan Oct 13 '16 at 18:15
  • 5
    In contrast to the `dropLast(..)` method [shown in @LeoDabus answer](http://stackoverflow.com/a/40028338/4573247), 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 the `dropLast` method is Swift native, `.substring` is bridged via Foundation from `NSString` (not entirely Swift native). – dfrib Oct 13 '16 at 20:06
  • wouldn't better question be, how to select the first N characters, otherwise you need to test you'd have enough to stay non-nil – aremvee Mar 12 '17 at 02:13
26

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"
Deepak Tagadiya
  • 2,187
  • 15
  • 28
2

Better to use removeLast()

var myString = "Hello world"
myString.removeLast(2)

output : "Hello wor"
Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29
Guillaume Ramey
  • 205
  • 2
  • 5
  • 2
    This 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." – koen Jun 24 '19 at 12:24
1

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?

Andy Obusek
  • 12,614
  • 4
  • 41
  • 62