Skip to content

Instantly share code, notes, and snippets.

@jianghaoyuan2007
Last active September 22, 2017 02:46
Show Gist options
  • Save jianghaoyuan2007/ab5804101f9298e4f318d7da71f025b2 to your computer and use it in GitHub Desktop.
Save jianghaoyuan2007/ab5804101f9298e4f318d7da71f025b2 to your computer and use it in GitHub Desktop.
The example for getting substring of string with a range in Swift 4.
import Foundation
var greeting = "Hello, Swift 4."
let startIndexOffsetBy = 7
let endIndexOffsetBy = -1
let startIndex = greeting.index(greeting.startIndex, offsetBy: startIndexOffsetBy)
let endIndex = greeting.index(greeting.endIndex, offsetBy: endIndexOffsetBy)
let rangeOfSubstring = [startIndex...endIndex]
let substring = String(greeting[startIndex...endIndex])
print(substring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment