Why does
"http://blah/".stringByAppendingPathComponent("foo")
return
"http:/blah/foo"
Notice the dropped forward slash.
If you read the documentation for stringByAppendingPathComponent, you will see this statement:
Note that this method only works with file paths (not, for example,
string representations of URLs).
The implementation of stringByAppendingPathComponent is "fixing" what it perceives as a badly formed file path.
You should either be using NSURLs or stringByAppendingString.
Method stringByAppendingPathComponent is supposed to be used with file paths and not with urls so my feeling is that method logic is considering // a mistake and fixing it.
You need to first convert your link to NSURL, then you can use URLByAppendingPathComponent:
if let blaFooURL = NSURL(string:"http://blah/")?.URLByAppendingPathComponent("foo") {
println(blaFooURL) // "http://blah/foo"
}
Related
I would like to find a regex formula that takes this URL
https://info.example.edu/programs/degree/page1/
and turns it into this
info.example.edu/programs/degree/page1/
I currently have this formula but it neglects the subdomain
=REGEXEXTRACT(A1,"(\..+)")
You could try this instead.
=REGEXEXTRACT(A1,"[^/]+//(.+)")
This captures anything after //
Output:
NSURL's URLByAppendingPathExtension: doesn't seem to be acting properly for me. I am trying to add a path to the end of an existing URL. For example:
[[NSURL URLWithString:#"https://www.example.com"] URLByAppendingPathExtension:#"/10392983/example"]
Should be returning an URL with http://www.example.com/10392983/example according to the documentation. Instead, it is escaping the slashes. I can achieve the same result converting to the URL to an absolute string and using regular NSString operations, but the above seems like it would be a lot more elegant. Any ideas as to which NSURL method to use to achieve this result or is this truly a bug?
For what you are trying to do, you should use:
URLByAppendingPathComponent
According to the docs, URLByAppendingPathExtension is for extensions like .html. It's a little vague, but from the docs:
If the original URL ends with one or more forward slashes, these are
removed from the returned URL. A period is inserted between the two
parts of the new URL.
This makes me think that this should only be used for appending things like .html and .php to URLs. Just use URLByAppendingPathComponent instead.
NSURLs can get confusing and weird.
Documentation for URLByAppendingPathExtension says that:
"If the original URL ends with one or more forward slashes, these are removed from the returned URL. A period is inserted between the two parts of the new URL."
"removed from the returned URL" implies that these '/' forward slash characters will be replaced with their escaped counterparts (that is a % sign for forward slashes).
UrlByAppendingPathComponent from #scott's answer does not appear to work in my tests? The method needs to be spelled 'URLByAppendingPathComponent' with a capitalized 'URL' and not the lowercase 'Url' .
Try this:
[NSURL URLWithString:[#"https://www.example.com" stringByAppendingPathComponent:#"/10392983/example"]]
That will produce the NSURL with a similar level of elegance-complexity. You can also tack on any number of "stringByAppendingPathComponent" methods to produce your NSURL.
Conversely here's an example of scott's code above:
NSURL*combined = [baseURL URLByAppendingPathComponent: stringPath];
What term do you give the part of the url after the last slash, but before the query? It seems most places people call it "the last part of the path" e.g. here but that is just so... wordy.
E.g. "something" in this url:
http://www.example.com/path/to/something?param=foo
Update:
I was hoping there was a well-known answer that I was not aware of, but it seems there is still some debate so, that's my answer right there. Guess I'll just keep calling it "the part of the path after the last slash". But I'll leave this question open anway, in case someone makes a convincing argument that gets lots of upvotes.
I'm a bit late to the party, but where I work we call it the slug, as mentioned here for example: https://prettylinks.com/2018/03/url-slugs/
protocol://server.domain/path?query
The path element (and it appears there is no 'defacto' definition) in my mind is the path to the resource on the server. No matter whether the resource is a file (blah.html) or a folder (/path/) it still instructs the server to use the path to find the resource.
Now there appears to be another definition at good/bad ol' Wikipedia here which states that it is usually "http://server/path/program?query_string" where the end resource is defined as 'program' but I think this is incorrect (is a folder a program?)
So.. perhaps its should be
protocol://server.domain/path[/resource.*]?query
? /../../ I traverse...
If URL is like /path/to/file.html or example.com/path/to/something.php?param=foo
then I think we can call it filename as mentioned at http://httpd.apache.org/docs/2.2/mod/directive-dict.html
In my experience it is called a query string whenever it is instructing action on the website. Code is frequently written to address the query string, which would prompt customized results on the page.
Otherwise, I agree it would be called a file name, if the path was referencing a file.
I saw a few uncertain suggestions here and there, including "filename", "basename". The Qt project has these names in their lexicon, so it can serve as a standard.
Namely check out the QFileInfo class.
They have standardized the names as the following:
QFileInfo fi("/tmp/archive.tar.gz");
fi.fileName(); // "archive.tar.gz"
fi.baseName(); // "archive"
fi.completeBaseName(); // "archive.tar"
fi.suffix(); // "gz"
fi.completeSuffix(); // "tar.gz"
This is a specification for local files however, so in this standard, there is a departure from basic URI terminology here:
fi.path(); // "/tmp"
fi.filePath(); // "/tmp/archive.tar.gz"
Where as in QUrl, you have path & fileName
QUrl("file:file.txt").path(); // "file.txt"
QUrl("/home/user/file.txt").path(); // "/home/user/file.txt"
QUrl("http://www.example.com/test/123").path(); // "/test/123"
and
QUrl("file:file.txt").fileName(); // "file.txt"
QUrl("/home/user/file.txt").fileName(); // "file.txt"
QUrl("http://www.example.com/test/123").fileName(); // "123"
If I were building a library, I personally would adopt QFileInfo terminology and apply to both URI and local files, and hope the entire rest of the world has enough sense to follow me.
I found the terms "last segment" or "last chunk" quite accurate and succinct.
I am facing a problem that one of my fields need to be shown in the url contains special character (/, \, :).
The stupid way to handle this generate action links by using UrlEncode(). Then UrlDecode is used before consuming in controller. But I think it really stupid because too many places need to be adapted.
So, my problem is there any way to extend the url route or just write my own one to achieve it?
Thanks,
Mike
You can extend the System.Web.Routing.Route object to create a custom route and override the GetRouteData and GetVirtualPath methods. These are called to resolve a route's values and create a URL from given route values, respectively. However, I don't think URLs can contain URL encoded values for / (%2f) within the path portion of a URL though it is ok in a query string.
I have a string that may have a path to a file. Example src="/folder/whatever". How do I replace that path with src="http://www.sitename.com/folder/whatever ?
If your string contains src="/...", possibly many times, do this:
string.gsub!(/\bsrc="(\/[^"]*)"/, 'src="http://www.sitename.com\1"')
If your string contains the URL only, do this:
src.replace('http://www.sitename.com' + src)
More information about String#gsub and String#gsub! here: http://www.ruby-doc.org/core/classes/String.html#M000832
route helpers. use url instead of path.
i like pts's solution, but i might remove the slash from the regex...so it would be:
string.gsub!(/\bsrc="([^"]*)"/, 'src="replacement_text\1"')
use the \1 to access the back reference