Skip to content

Instantly share code, notes, and snippets.

@ryantxr
Last active March 12, 2016 21:38
Show Gist options
  • Save ryantxr/e46639e72508dce3428c to your computer and use it in GitHub Desktop.
Save ryantxr/e46639e72508dce3428c to your computer and use it in GitHub Desktop.
// Approach #1
let fruit = ["012Apple", "03218Banana", "11 Orange"]
var stringsOnly: [String] = []
for f in fruit {
stringsOnly.append(f.stringByTrimmingCharactersInSet(NSCharacterSet.letterCharacterSet().invertedSet))
}
// Approach #2
let fruits = ["012Apple", "03218Banana", "11 Orange"]
var stringOnly = [String]()
for fruit in fruits {
if let match = fruit.rangeOfString("[a-zA-Z]+", options: .RegularExpressionSearch) {
stringOnly.append(fruit.substringWithRange(match))
}
}
print(stringOnly) // Apple, Banana, Orange
// result: ["Apple", "Banana", "Orange"]
// Credit: http://stackoverflow.com/questions/35963208/trim-numerical-and-separator-values-from-the-beginning-of-strings-in-swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment