如何使用 ios 中的子字符串拆分 url?
How to Split a url using substring in ios?
如何用 /
字符拆分 URL?
例如,
www.whosebug.com/questions
我想在上面的/
之后拆分URL得到/questions
这可能对你有帮助。
NSString *url = @"www.whosebug.com/questions";
NSArray *items = [url componentsSeparatedByString:@"/"];
NSString *str1=[items objectATindex:0]; //www.whosebug.com
NSString *str2=[items objectATindex:1]; //questions
在 NSURL
对象上使用 pathComponents
属性。
This property contains an array containing the individual path components of the URL, each unescaped using the stringByReplacingPercentEscapesUsingEncoding:
method. For example, in the URL file:///directory/directory%202/file, the path components array would be @[@"/", @"directory", @"directory 2", @"file"].
NSURL *myUrl = [NSURL URLWithString:@"www.whosebug.com/questions"];
NSString *lastPathComponent = [myUrl lastPathComponent]; // "questions"
NSArray *pathComponents = [myUrl pathComponents]; // <__NSArrayM 0x7fc45b649df0>( www.whosebug.com, questions )
要删除 http://
,请使用 host
属性:
NSURL *myUrl = [NSURL URLWithString:@"http://www.whosebug.com/questions"];
NSString *host = myUrl.host; // whosebug.com
如何用 /
字符拆分 URL?
例如,
www.whosebug.com/questions
我想在上面的/
之后拆分URL得到/questions
这可能对你有帮助。
NSString *url = @"www.whosebug.com/questions";
NSArray *items = [url componentsSeparatedByString:@"/"];
NSString *str1=[items objectATindex:0]; //www.whosebug.com
NSString *str2=[items objectATindex:1]; //questions
在 NSURL
对象上使用 pathComponents
属性。
This property contains an array containing the individual path components of the URL, each unescaped using the
stringByReplacingPercentEscapesUsingEncoding:
method. For example, in the URL file:///directory/directory%202/file, the path components array would be @[@"/", @"directory", @"directory 2", @"file"].
NSURL *myUrl = [NSURL URLWithString:@"www.whosebug.com/questions"];
NSString *lastPathComponent = [myUrl lastPathComponent]; // "questions"
NSArray *pathComponents = [myUrl pathComponents]; // <__NSArrayM 0x7fc45b649df0>( www.whosebug.com, questions )
要删除 http://
,请使用 host
属性:
NSURL *myUrl = [NSURL URLWithString:@"http://www.whosebug.com/questions"];
NSString *host = myUrl.host; // whosebug.com