Created
February 12, 2016 18:04
-
-
Save mingsai/2b14d550cda420b18f57 to your computer and use it in GitHub Desktop.
A Swift extension to perform a number of common string procedures.
This file contains hidden or 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
| // | |
| // MNGStringExtensions.swift | |
| // | |
| // | |
| // Created by Tommie Carter on 6/29/15. | |
| // Copyright © 2015 MING Technology. All rights reserved. | |
| // | |
| import Foundation | |
| extension String { | |
| var NS: NSString { return (self as NSString) } | |
| func sliceFrom(start: String, to: String) -> String? { | |
| return (rangeOfString(start)?.endIndex).flatMap { sInd in | |
| (rangeOfString(to, range: sInd..<endIndex)?.startIndex).map { eInd in | |
| substringWithRange(sInd..<eInd) | |
| } | |
| } | |
| } | |
| var isEmail: Bool { | |
| do { | |
| let regex = try NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .CaseInsensitive) | |
| return regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) != nil | |
| } catch { | |
| return false | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment