122

I need to seperate the last 4 letters of a string. How can I seperate it? The length of string is changing.

Example:

var a = "StackOverFlow"
var last4 = a.lastFour // That's what I want to do
print(last4) // prints Flow
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
do it better
  • 4,627
  • 6
  • 25
  • 41
  • substring function.. – Avinash Raj Sep 29 '15 at 17:03
  • @EricAya and other closers: This is not a duplicate of the linked question. In method call chains like `[1,2,3].map({"Int(\($0))"}).joined(separator: ", ").last(x)`, `substringWithRange` and friends are useless. – Raphael Apr 07 '17 at 11:49
  • See [here](http://stackoverflow.com/q/43277938/539599) for a solution that works in method chains, i.e. without an external reference to the string itself. (Commenting here because this is the top Google result.) – Raphael Apr 07 '17 at 13:12

2 Answers2

324

Swift 2:

A solution is substringFromIndex

let a = "StackOverFlow"
let last4 = a.substringFromIndex(a.endIndex.advancedBy(-4))

or suffix on characters

let last4 = String(a.characters.suffix(4))

Swift 3:

In Swift 3 the syntax for the first solution has been changed to

let last4 = a.substring(from:a.index(a.endIndex, offsetBy: -4))

Swift 4+:

In Swift 4 it becomes more convenient:

let last4 = a.suffix(4)

The type of the result is a new type Substring which behaves as a String in many cases. However if the substring is supposed to leave the scope where it's created in you have to create a new String instance.

let last4 = String(a.suffix(4))
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 1
    you da ma, all over the Swift strings! Can't find a string question without you on it – MikeG Dec 06 '16 at 01:39
  • 2
    be aware that substring will crash if the string is less than 4 characters long – codeinthesky Jan 10 '17 at 05:00
  • Unfortunately, this is useless in a chain like `[1,2,3].map({"Int(\($0))"}).joined(separator: ", ").last(x)` -- we don't have a handle on the string in such situations! – Raphael Apr 07 '17 at 11:48
  • 1
    @Raphael `String([1,2,3].map({"Int(\($0))"}).joined(separator: ", ").characters.suffix(4))` does not work? – vadian Apr 07 '17 at 12:35
  • @vadian Let's pretend we are in the middle of a chain. ;) – Raphael Apr 07 '17 at 12:44
  • @Raphael That's beyond the scope of this *last 4 characters* question. – vadian Apr 07 '17 at 13:04
  • Well, the question broad enough; I don't think it should have closed as duplicate. Of course, any answer may be as narrow as it wished; I just wanted to leave this comment since this is the top Google hit for me. – Raphael Apr 07 '17 at 13:11
  • Thanks very much for this answer Vadian ! Your solution is so simple ! When we see the Apple documentation on it, we want to cry ... – Fox5150 Jan 24 '18 at 14:21
17
String substr = a.substring(a.length() - 4)

syntax is wrong. no type before vars in Swift.

let a = "1234567890"
let last4 = String(a.characters.suffix(4))
print(last4)

works on Swift 3.0

ingconti
  • 10,876
  • 3
  • 61
  • 48
sparsh610
  • 1,552
  • 3
  • 27
  • 66