网址减去Objective-C中的查询字符串 [英] Url minus query string in Objective-C

查看:78
本文介绍了网址减去Objective-C中的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中获取网址减去查询字符串的最佳方法是什么?一个例子:

What's the best way to get an url minus its query string in Objective-C? An example:

输入:

http://www.example.com/folder/page.htm?param1=value1&param2=value2

输出:

http://www.example.com/folder/page.htm

我是否缺少NSURL方法来做到这一点?

Is there a NSURL method to do this that I'm missing?

推荐答案

我没有看到NSURL方法.您可以尝试以下方法:

There's no NSURL method I can see. You might try something like:

NSURL *newURL = [[NSURL alloc] initWithScheme:[url scheme]
                                         host:[url host]
                                         path:[url path]];

测试看起来不错:

#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
    NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];

    NSURL *url = [NSURL URLWithString:@"http://www.abc.com/foo/bar.cgi?a=1&b=2"];
    NSURL *newURL = [[[NSURL alloc] initWithScheme:[url scheme]
                                              host:[url host]
                                              path:[url path]] autorelease];
    NSLog(@"\n%@ --> %@", url, newURL);
    [arp release];
    return 0;
}

运行此操作会产生:

$ gcc -lobjc -framework Foundation -std=c99 test.m ; ./a.out 
2010-11-25 09:20:32.189 a.out[36068:903] 
http://www.abc.com/foo/bar.cgi?a=1&b=2 --> http://www.abc.com/foo/bar.cgi

这篇关于网址减去Objective-C中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆