如何从 NSURL 末尾删除 3 个字符?
我的第一个问题!我已经能够在没有寻求帮助的情况下编写这个 RSS 阅读器的大部分内容(通过 stackoverflow 进行大量搜索!),但我在这里被难住了。
NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]];
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
urlbase = [filteredArray componentsJoinedByString:@" "];
NSLog(@"%@ %i" , urlbase, 4353);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]];
links3 数组是一个带有字符串的 NSMutableArray。前几行完美地消除了该数组中每个字符串开头的空格,该数组存储在“urlbase”中,因此它们出来时看起来很好。当我们 NSLog urlbase 时,我们看到:
http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE
但是,当我们使用: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]
我们看到: http://www.feedzilla.com /r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE%0A
如何解决此问题?我可以以某种方式删除那些尾部元素吗?谢谢!
工作代码:
NSMutableArray *links3 = [[NSMutableArray alloc] init];
RSSAppDelegate *appDelegate = (RSSAppDelegate *)[[UIApplication sharedApplication] delegate];
links3 = appDelegate.links;
NSLog(@"%@ %i", [links3 objectAtIndex:indexPath.row], 3453);
NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]];
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
urlbase = [filteredArray componentsJoinedByString:@" "];
NSLog(@"%@ %i" , urlbase, 4353);
NSLog(@"%@ %i" , urlbase, 345346);
NSString* fixed = [urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSURL *hellothere = [NSURL URLWithString:fixed];
NSLog(@"%@ %i", hellothere, 54);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:fixed]];
嘿,我没说它很漂亮。谢谢迪德里克·胡根布姆!
my first question! I've been able to code up most of this RSS reader without enlisting help (through a lot of searches through stackoverflow!) but I'm stumped here.
NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]];
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
urlbase = [filteredArray componentsJoinedByString:@" "];
NSLog(@"%@ %i" , urlbase, 4353);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]];
The links3 array is a NSMutableArray
with strings. The first few lines work flawlessly in eliminating the space at the beginning each string from that array, which is stored in 'urlbase' so they look fine when they come out. When we NSLog urlbase, we see:
http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE
But, when we use: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]
We see: http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE%0A
How can I fix this? Can I remove those tail elements somehow? Thanks!
Working code:
NSMutableArray *links3 = [[NSMutableArray alloc] init];
RSSAppDelegate *appDelegate = (RSSAppDelegate *)[[UIApplication sharedApplication] delegate];
links3 = appDelegate.links;
NSLog(@"%@ %i", [links3 objectAtIndex:indexPath.row], 3453);
NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]];
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
urlbase = [filteredArray componentsJoinedByString:@" "];
NSLog(@"%@ %i" , urlbase, 4353);
NSLog(@"%@ %i" , urlbase, 345346);
NSString* fixed = [urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSURL *hellothere = [NSURL URLWithString:fixed];
NSLog(@"%@ %i", hellothere, 54);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:fixed]];
Hey, I didn't say it was pretty. Thanks Diederik Hoogenboom!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
URL 字符串的最后一部分是换行符。
Try this:
The last part of your URL string is a line feed.