Remove ? from query string swift 4 - swift

I have a url in which I add URL components to it to form a query string. For example the url is https://example.com/test, and with the url components the final url is https://example.com/test?urlcomponent1=1&urlcomponent2=1234.
I need to keep the final url with the urlcomponents, but I need to remove the ?. How do I do that? So the final url would be https://example.com/testurlcomponent1=1&urlcomponent2=1234.
I have looked into removing artifacts, but haven't found a solution.

If you know you only have one ? in your url, you can remove it using replacingOccurrencesOf
let newURL = URL(string: url.absoluteString.replacingOccurrences(of: "?", with: ""))

Related

URL(filePath: strPath) vs URL(fileURLWithPath: strPath)

let url = URL(filePath: pathString!)
let url2 = URL(fileURLWithPath: pathString!)
I tried both and they worked both.
What's the difference between the two ways creating such an URL-object?
String file paths are outmoded. Use file URLs. And not file URLs derived from a string file path; construct or obtain the URL legitimately. So the real answer is: don't use either of those methods.

Extracting string after x number of backlashes in swift

I can't find any way to extract a certain string value from another string in SwiftUi.
It is the following link:
"http://media.site.com/videos/3070/0003C305B74F77.mp4"
How would you go about extracting the numbers 0003C305B74F77?
It would be much easier to treat it as an URL. That's what it is. All you need it to get its last path component after deleting its path extension.
let link = "http://media.site.com/videos/3070/0003C305B74F77.mp4"
if let url = URL(string: link) {
let lastPathComponent = url.deletingPathExtension().lastPathComponent
print(lastPathComponent) // "0003C305B74F77"
}

contentsof:url loads url content of a truncated URL

When I use contentsof:url it truncates the url before retrieving the content, resulting in different html content than the displayed in the WKWebView.
For example
contents = try String(contentsOf: https://www.amazon.com/gp/aw/d/B00BECJ4R8/ref=mp_s_a_1_1?ie=UTF8&qid=1531620716&sr=8-1-spons&pi=AC_SX236_SY340_QL65&keywords=cole+haan&psc=1)
returns the contents of this page: https://www.amazon.com/gp/aw/d/B00BECJ4R8/
Why is this happening? Is there an alternative method that allow you to read the content of the actual URL not the truncated URL?
Any advice if very much appreciated.
Thank you.
You shouldn't be using String(contentsOf:) to load a website. You should use the URL Loading System for this work then passing that object back to your webView.load(_:) method in viewDidLoad()
let urlString = "https://www.amazon.com/dp/B00BECJ4R8/?tag=stackoverflow17-20"
// URL construct may fail in case of the String not being a properly formatted URL, so unwrap it.
if let url = URL(string: urlString) {
// Create a URLRequest instance with the new url.
let request = URLRequest(url: url)
// Load the request.
webView.load(request)
}

Find specific character from URL Link

How can I find out specific character from URL String variable using Swift 3?
i want to get the value out of the URLlink(image is given) user_lati,user_long,destination_lat,destinaton_long and save it.
Can any one give me the solution for this?
I have given a couple of examples below. You should really check the documentation for String, URL and Collections
let urlString = "https://www.example.com/search?q=test&user=john"
let url = URL(string: urlString)!
print(url.query) // q=test&user=john
print(urlString.index(of: "?")?.encodedOffset) // 30 - index of ?
print(url.absoluteString.index(of: "?")?.encodedOffset) // same as above

Delete parameter from json URL with swift3

I have a URL that looks like 123456_https://example.com.
I want to delete every thing before the _https: part. At the end, the URL should look like https://example.com.
How can I achieve that?
If the prefix is always a couple of characters ending with a underscore you can use this regular expression
let url = "123456_https://example.com"
let trimmedURL = url.replacingOccurrences(of: "^\\w+_", with: "", options: .regularExpression)
you can do something like this if you do not know how to use Regular Expressions
// url string
var url = "123456_https://example.com"
// seperate url by "_" and provides 123456 and https://example.com in urlArray
let urlArray = url.components(separatedBy: "_")
// now to be safe
if(urlArray.count == 2){
// here you get your desired string
let myUrl = urlArray[1]
}