Skip to main content
3 of 9
added 152 characters in body
vadian
  • 286.2k
  • 32
  • 378
  • 379

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)) 

code is Swift 2

Edit:

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

let last4 = a.substring(from:a.index(a.endIndex, offsetBy: -4)) 
vadian
  • 286.2k
  • 32
  • 378
  • 379