Created
April 13, 2015 12:27
-
-
Save JadenGeller/4e021818ae9cf9f81f9f to your computer and use it in GitHub Desktop.
Replace Prefix
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
| extension String { | |
| func replacePrefix(prefix: String, replacement: String) -> String { | |
| if hasPrefix(prefix) { | |
| return replacement + substringFromIndex(prefix.endIndex) | |
| } | |
| else { | |
| return self | |
| } | |
| } | |
| } | |
| // Examples | |
| "Apple".replacePrefix("App", replacement: "Program") // -> Programle | |
| "Google".replacePrefix("App", replacement: "Program") // -> Google |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment