I have an URL that looks like myapp://jhb/test/deeplink/url?id=4567 .
I want to delete every thing after the ? char. At the end the URL should look like myapp://jhb/test/deeplink/url. how. can I achieve that? convert the url to a string? Regex?
-
Just look out this one stackoverflow.com/questions/39184984/…Bhupat Bheda– Bhupat Bheda2017-10-09 12:16:13 +00:00Commented Oct 9, 2017 at 12:16
Add a comment
|
5 Answers
can I achieve that? convert the url to a string? Regex?
When working with URLs, it would be better to treat it as URLComponent:
A structure that parses URLs into and constructs URLs from their constituent parts.
therefore, referring to URLComponent what are you asking is to remove the the query subcomponent from the url:
if var components = URLComponents(string: "myapp://jhb/test/deeplink/url?id=4567") {
components.query = nil
print(components) // myapp://jhb/test/deeplink/url
}
Note that query is an optional string, which means it could be nil (as mentioned in the code snippet, which should leads to your desired output).