Skip to content

Instantly share code, notes, and snippets.

@lanserxt
Created October 12, 2018 19:22
Show Gist options
  • Save lanserxt/901386f923e06e1c31508ca9e9761a5f to your computer and use it in GitHub Desktop.
Save lanserxt/901386f923e06e1c31508ca9e9761a5f to your computer and use it in GitHub Desktop.
URL strip
func stripUrlToValid(_ url: String) -> String {
var stripped = url.trimmingCharacters(in: .whitespacesAndNewlines)
if stripped.hasPrefix("https") {
stripped = stripped.replacingOccurrences(of: "https", with: "")
}
if stripped.hasPrefix("http") {
stripped = stripped.replacingOccurrences(of: "http", with: "")
}
while stripped.hasPrefix(":") || stripped.hasPrefix("/") {
if stripped.hasPrefix(":") {
stripped = String(stripped.dropFirst(1))
}
if stripped.hasPrefix("/") {
stripped = String(stripped.dropFirst(1))
}
}
if let firstBrake = stripped.firstIndex(of: "/") {
stripped = String(stripped.prefix(upTo: firstBrake))
}
print("|\(url)| -> |\(stripped)|")
return url
}
stripUrlToValid("http://abc.com/")
stripUrlToValid("https://abc.com")
stripUrlToValid("http://abc.com/valid")
stripUrlToValid("https://abc.com/login")
stripUrlToValid("://abc.com/login")
stripUrlToValid("//abc.com/login")
stripUrlToValid("/abc.com/login")
stripUrlToValid("https//abc.com/login")
stripUrlToValid("http:/abc.com/login")
stripUrlToValid("https:///abc.com/login/status")
stripUrlToValid("abc.com/login")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment