Skip to content

Instantly share code, notes, and snippets.

@novinfard
Created June 18, 2018 15:56
Show Gist options
  • Save novinfard/035ff7f39a4b1d0d5815eab9e73f0a32 to your computer and use it in GitHub Desktop.
Save novinfard/035ff7f39a4b1d0d5815eab9e73f0a32 to your computer and use it in GitHub Desktop.
[Working with Strings]
import Foundation
var stringOne = "Hello llollo"
var stringTwo = ""
stringOne.isEmpty
stringTwo.isEmpty
stringOne == "Hello llollo"
stringOne == "hello"
stringOne.hasPrefix("He")
stringOne.hasSuffix("He")
stringOne.replacingOccurrences(of: "ll", with: "pp")
var path = "/one/two/three/four"
//Create start and end indexes
let startIndex = path.index(path.startIndex, offsetBy: 4)
let endIndex = path.index(path.startIndex, offsetBy: 14)
let sPath = path[startIndex..<endIndex] //returns the String /two/three
//convert the substring to a string
let newStr = String(sPath)
path.substring(to:startIndex) //returns the String /one
path[..<startIndex] // same in Swift 4
path.substring(from:endIndex) //returns the String /four
path[endIndex...] // same in Swift 4
path.last
path.first
//get the length of the string
var length = path.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment