Mel*_*ina 114 objective-c nsurl nsstring
我有一个值为的NSString
http://digg.com/news/business/24hr
Run Code Online (Sandbox Code Playgroud)
如何在第3级之前获得所有内容?
http://digg.com/news/
Run Code Online (Sandbox Code Playgroud)
zne*_*eak 427
请注意,这不完全是第三级.URL就像这样分割:
http)://分隔符username:password@hostname)digg.com):80例如在域名之后)/news/business/24hr)?foo=bar&baz=frob)#foobar)."功能齐全"的网址如下所示:
http://foobar:nicate@example.com:8080/some/path/file.html;params-here?foo=bar#baz
Run Code Online (Sandbox Code Playgroud)
NSURL有各种各样的访问者.您可以在NSURL课程文档中查看它们,访问URL部分.供快速参考:
-[NSURL scheme] = http-[NSURL resourceSpecifier] =(从//到URL末尾的所有内容)-[NSURL user] = foobar-[NSURL password] = nicate-[NSURL host] = example.com-[NSURL port] = 8080-[NSURL path] = /some/path/file.html-[NSURL pathComponents] = @ ["/","some","path","file.html"](请注意,初始/是其中的一部分)-[NSURL lastPathComponent] = file.html-[NSURL pathExtension] = html-[NSURL parameterString] = params-在这里-[NSURL query] = foo = bar-[NSURL fragment] =巴兹但是你想要的是这样的:
NSURL* url = [NSURL URLWithString:@"http://digg.com/news/business/24hr"];
NSString* reducedUrl = [NSString stringWithFormat:
@"%@://%@/%@",
url.scheme,
url.host,
url.pathComponents[1]];
Run Code Online (Sandbox Code Playgroud)
对于您的示例URL,您似乎需要的是协议,主机和第一个路径组件.(返回的数组中索引0处的元素-[NSString pathComponents]只是"/",因此您需要索引1处的元素.其他斜杠将被丢弃.)
| 归档时间: |
|
| 查看次数: |
63508 次 |
| 最近记录: |