【问题标题】:How do I remove 3 characters from the end of an NSURL?如何从 NSURL 末尾删除 3 个字符?
【发布时间】:2011-02-20 04:34:18
【问题描述】:

我的第一个问题!我已经能够在不寻求帮助的情况下编写大部分这个 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!

【问题讨论】:

  • %0A 是换行符(行尾)。它可能已经在 urlbase 中,您只是在 NSLog 中看不到它,因为它是空格。看看你是怎么把它放在那里的。

标签: iphone objective-c ipod-touch


【解决方案1】:

试试这个:

[urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]

URL 字符串的最后一部分是换行符。

【讨论】:

  • 很高兴听到!您能否将此答案标记为解决此问题的答案,谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-04-03
  • 2010-12-23
  • 2010-11-05
  • 2011-07-14
  • 2017-01-06
相关资源
最近更新 更多