Skip to main content
Added a "Swift 2" heading.
Source Link
Adil Hussain
  • 32.6k
  • 24
  • 127
  • 170

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

code is Swift 2


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

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


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

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)) 
added 1 character in body
Source Link
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


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 44+:

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

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


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

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


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)) 
deleted 12 characters in body
Source Link
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


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 Apple recommendsyou have to create a regularnew String instance.

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

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


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 Apple recommends to create a regular String instance.

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

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


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)) 
Fixed typo.
Source Link
Martin R
  • 541.4k
  • 98
  • 1.3k
  • 1.4k
Loading
added 304 characters in body
Source Link
vadian
  • 286.2k
  • 32
  • 378
  • 379
Loading
added 92 characters in body
Source Link
vadian
  • 286.2k
  • 32
  • 378
  • 379
Loading
added 152 characters in body
Source Link
vadian
  • 286.2k
  • 32
  • 378
  • 379
Loading
added 81 characters in body
Source Link
vadian
  • 286.2k
  • 32
  • 378
  • 379
Loading
Post Undeleted by vadian
Post Deleted by vadian
Source Link
vadian
  • 286.2k
  • 32
  • 378
  • 379
Loading