Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created April 13, 2015 12:27
Show Gist options
  • Select an option

  • Save JadenGeller/4e021818ae9cf9f81f9f to your computer and use it in GitHub Desktop.

Select an option

Save JadenGeller/4e021818ae9cf9f81f9f to your computer and use it in GitHub Desktop.
Replace Prefix
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