Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension String {
- func word(at index: Int) -> String {
- do {
- let regex = try NSRegularExpression(pattern: "([A-Z]|[a-z]|[0-9])\\w+", options: .caseInsensitive)
- let matches = regex.matches(in: self, options: .reportCompletion, range: NSRange(location: 0, length: self.count)).map { regexMatch -> Substring in
- let range = regexMatch.range
- return self[self.index(self.startIndex, offsetBy: range.location)..<self.index(self.startIndex, offsetBy: range.location + range.length)]
- }
- guard matches.count > index else {
- return ""
- }
- return String(matches[index])
- } catch let error {
- return ""
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement