Skip to content

Instantly share code, notes, and snippets.

@mitchellporter
Created October 30, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchellporter/e84a1b45f48b50a96c4a to your computer and use it in GitHub Desktop.
Save mitchellporter/e84a1b45f48b50a96c4a to your computer and use it in GitHub Desktop.
var str = "Hello Swift!"
var aStr = "Hello World!"
str.startIndex // first index
str.endIndex // end index
str.startIndex.distanceTo(str.endIndex) // string length
str.characters.count // string length
str[str.startIndex.advancedBy(4)] // get character at index 4
str[str.startIndex.advancedBy(4)...str.startIndex.advancedBy(8)] // get characters in range index 4 to 8
str.characters.last // retrieve last letter
str.characters.first // retrieve first letter
str.removeAtIndex(str.characters.indices.first!) // remove first letter
str.removeAtIndex(str.characters.indices.last!) // remove last letter
let first = str.characters.dropFirst()
String(first) // dropFirst
let last = str.characters.dropLast()
String(last) // dropLast
"aeiou".characters.contains("a") // contains character
str.characters.filter{!"aeiou".characters.contains($0)} // remove vowels
str.characters.indices // retrieve the Range value for string
str.isEmpty // test whether there is anything in the string
str.startIndex.advancedBy(5) // advance index
str.substringToIndex(str.startIndex.advancedBy(5)) // returns string up to the 5th character
str.substringFromIndex(str.startIndex.advancedBy(5)) // returns string from the 5th character
min("antelope","ant") // returns the alphabetical first
max("antelope","ant") // returns the alphabetical last
str.characters.prefix(5) // returns first 5 characters
str.characters.reverse() // return reverse array of Characters
str.characters.suffix(5) // returns last 5 characters
swap(&str, &aStr) // swaps two strings for the value of one another
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment