无法从 NSString 中删除空格

Can't remove whitespaces from NSString

我有一个 url - NSString - 我从 RSS 提要中获得的。不幸的是,它包含很多白色 space,我无法使用以下代码将其删除。几个月来,我一直在为同一项工作使用相同的代码,但这次它无法正常用于此提要。不知何故也会有一个白色的spaces,但这些不是/n-s。当我单击它在数据库中的行并尝试使用方向键从右到左浏览它时,每次我点击左箭头方向键而不是 1 时,光标跳转 4-5 白色 spaces白色space 每次按下按钮。有谁知道我应该如何处理这种情况?是否可以删除所有其他类型的 whitespaces?或者最好的方法是什么?

  NSString *stringForUrl = [feedObject[@"link"] stringByTrimmingCharactersInSet:
                                    [NSCharacterSet whitespaceCharacterSet]];

    stringForUrl = [stringForUrl stringByReplacingOccurrencesOfString:@"\n" withString:@""];           
    stringForUrl = [stringForUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
    stringForUrl = [stringForUrl stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

试试下面的方法:

NSCharacterSet *whitespaces = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];

NSArray *parts = [stringForUrl componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
finalString = [filteredArray componentsJoinedByString:@""];

从这里修改:Collapse sequences of white space into a single character and trim string