Last active
March 12, 2016 21:38
-
-
Save ryantxr/e46639e72508dce3428c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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