Skip to content

Instantly share code, notes, and snippets.

@ryantxr
Created March 11, 2016 23:01
Show Gist options
  • Save ryantxr/86331040318743dc8a97 to your computer and use it in GitHub Desktop.
Save ryantxr/86331040318743dc8a97 to your computer and use it in GitHub Desktop.
Get substring for a string by searching for a character
// Given the string "Fox is in the hole", how to get a substring up to the first space.
let myMessage = "Fox is in the hole"
var result: String?
if let index = myMessage.characters.indexOf(" ") {
result = myMessage.substringToIndex(index)
}
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment