替换数组中的字符串

时间:2010-09-27 19:23:09

标签: iphone objective-c cocoa arrays string

    NSArray *ArtistNames = [RawData componentsMatchedByRegex:regEx1]; 
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"'"];
    ArtistNames = [[ArtistNames componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @"'"];

此刻我的代码基本上我不能使用它,因为ArtistNames是一个数组而不是字符串,我将如何通过它?

2 个答案:

答案 0 :(得分:7)

我认为你是以错误的顺序调用你的方法。尝试将其分成多个语句。

// The separator should be some string of characters guaranteed to not be in any of the strings in the array.
#define SEPARATOR @"---***---"

NSArray *artistNames = [RawData componentsMatchedByRegex:regEx1]; 
NSString *doNotWant = @"'"
NSString *combinedNames = [artistNames componentsJoinedByString:SEPARATOR];
combinedNames = [combinedNames stringByReplacingOccurrencesOfString:doNotWant withString:@""];
artistNames = [combinedNames componentsSeparatedByString:SEPARATOR];

答案 1 :(得分:1)

为什么不绕过数组并自己创建第二个数组呢?

相关问题