Created
October 12, 2018 19:22
-
-
Save lanserxt/901386f923e06e1c31508ca9e9761a5f to your computer and use it in GitHub Desktop.
URL strip
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
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