如何在ios中使用子字符串拆分url?
如何按/字符拆分URL 比如说,如何在ios中使用子字符串拆分url?,ios,objective-c,nsstring,nsurl,Ios,Objective C,Nsstring,Nsurl,如何按/字符拆分URL 比如说, www.stackoverflow.com/questions 我想在上面的URL后/中拆分以获取/问题这可能会对您有所帮助 NSString *url = @"www.stackoverflow.com/questions"; NSArray *items = [url componentsSeparatedByString:@"/"]; NSString *str1=[items objectATindex:0]; //www.stackoverfl
www.stackoverflow.com/questions
我想在上面的URL后/中拆分以获取/问题这可能会对您有所帮助
NSString *url = @"www.stackoverflow.com/questions";
NSArray *items = [url componentsSeparatedByString:@"/"];
NSString *str1=[items objectATindex:0]; //www.stackoverflow.com
NSString *str2=[items objectATindex:1]; //questions
在NSURL对象上使用该属性
此属性包含一个数组,该数组包含URL的各个路径组件,每个组件都使用该方法进行了未缩放。例如,在URL中file:///directory/directory%202/file,路径组件数组将为@[@/,@directory,@directory 2,@file]
拆分后您想做什么?将其分成两个单独的stirng?拆分后,我想在两个标签中显示两个字符串。拆分后,我想在两个标签中显示拆分后的字符串。可能重复感谢它对我的帮助,以及如何在url之前删除http://in。我将尝试你的想法,感谢您回答我的问题。@Saravanakumar编辑了我的答案,以演示如何删除前缀。谢谢,我将尝试。此答案不是执行此任务的正确方法。NSURL拥有获取URL各个部分所需的所有方法。不需要进行字符串拆分。@rmaddy你完全正确。编辑我的答案以使用NSURL的主机属性。
NSURL *myUrl = [NSURL URLWithString:@"www.stackoverflow.com/questions"];
NSString *lastPathComponent = [myUrl lastPathComponent]; // "questions"
NSArray *pathComponents = [myUrl pathComponents]; // <__NSArrayM 0x7fc45b649df0>( www.stackoverflow.com, questions )
NSURL *myUrl = [NSURL URLWithString:@"http://www.stackoverflow.com/questions"];
NSString *host = myUrl.host; // stackoverflow.com