如何从NSURL的末尾删除3个字符?

时间:2010-05-16 21:13:28

标签: iphone objective-c ipod-touch

我的第一个问题!我已经能够在没有帮助的情况下编写大部分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]];
嘿,我没说它很漂亮。谢谢Diederik Hoogenboom!

1 个答案:

答案 0 :(得分:1)

试试这个:

[urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]

您的网址字符串的最后一部分是换行符。