如何使用NSScanner或componentsSeperatedByString

时间:2011-02-06 15:57:32

标签: iphone objective-c

我将{“Red”,“Blue”,“Green”,“Yellow”}作为字符串返回。如何处理这个以添加到数组?

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString* sampleString = @"{\"Red\",\"Blue\",\"Green\",\"Yellow\"}";
    NSArray* components = [sampleString componentsSeperatedByString:@"\"{"];

    [pool drain];
    return 0;
}

更新了代码#

NSString* sampleString = @"{\"Red\",\"Blue\",\"Green\",\"Yellow\"}";

NSMutableArray *rows = [NSMutableArray array];

// Get newline character set
NSMutableCharacterSet *removeCharacterSet = (id)[NSMutableCharacterSet characterSetWithCharactersInString:@"{(,}"];
[removeCharacterSet formIntersectionWithCharacterSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];

// Characters that are important to the parser
NSMutableCharacterSet *importantCharactersSet = (id)[NSMutableCharacterSet characterSetWithCharactersInString:@"\""];
[importantCharactersSet formUnionWithCharacterSet:removeCharacterSet];

// Create scanner, and scan string
NSScanner *scanner = [NSScanner scannerWithString:sampleString];
[scanner setCharactersToBeSkipped:nil];
while ( ![scanner isAtEnd] ) 
{        
    BOOL insideQuotes = NO;
    BOOL finishedRow = NO;
    NSMutableArray *columns = [NSMutableArray arrayWithCapacity:10];
    NSMutableString *currentColumn = [NSMutableString string];
    while ( !finishedRow ) 
    {
        NSString *tempString;
        if ( [scanner scanUpToCharactersFromSet:importantCharactersSet intoString:&tempString] ) {
            [currentColumn appendString:tempString];
        }

        if ( [scanner isAtEnd] ) {
            if ( ![currentColumn isEqualToString:@""] ) [columns addObject:currentColumn];
            finishedRow = YES;
        }
        else if ( [scanner scanCharactersFromSet:removeCharacterSet intoString:&tempString] ) {
            if ( insideQuotes ) {
                // Add line break to column text
                [currentColumn appendString:tempString];
            }
            else {
                // End of row
                if ( ![currentColumn isEqualToString:@""] ) [columns addObject:currentColumn];
                finishedRow = YES;
            }
        }
        else if ( [scanner scanString:@"\"" intoString:NULL] ) {
            if ( insideQuotes && [scanner scanString:@"\"" intoString:NULL] ) {
                // Replace double quotes with a single quote in the column string.
                [currentColumn appendString:@"\""]; 
            }
            else {
                // Start or end of a quoted string.
                insideQuotes = !insideQuotes;
            }
        }
        else if ( [scanner scanString:@"," intoString:NULL] ) {  
            if ( insideQuotes ) {
                [currentColumn appendString:@","];
            }
            else {
                // This is a column separating comma
                [columns addObject:currentColumn];
                currentColumn = [NSMutableString string];
                [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
            }
        }
    }
    if ( [columns count] > 0 ) [rows addObject:columns];
}
NSLog(@"This String:%@",[rows objectAtIndex:0]);

我从http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data获得了代码。现在输出是这个字符串:(红色),如何摆脱“(”“)”?

1 个答案:

答案 0 :(得分:1)

以下是使用NSScanner实例扫描您提供的示例所需的全部内容:

NSScanner *scanner = [NSScanner scannerWithString:@"{\"Red\",\"Blue\",\"Green\",\"Yellow\"}"];
NSMutableCharacterSet *charactersToSkip = [NSMutableCharacterSet punctuationCharacterSet];
[scanner setCharactersToBeSkipped:charactersToSkip]; 
NSMutableArray *substrings = [NSMutableArray array];
NSString *substring = @"";
while (![scanner isAtEnd]) {
    [scanner scanUpToCharactersFromSet:charactersToSkip intoString:&substring];
    [scanner scanCharactersFromSet:charactersToSkip intoString:NULL];
    [substrings addObject:substring];
}

NSLog(@"%@", substrings);

请注意,如果您使用花括号代替parens,那么您需要做的就是从样本中创建一个字符串数组:

NSString *sampleString = @"(\"Red\",\"Blue\",\"Green\",\"Yellow\")";
NSArray *strings = [sampleString propertyList];
NSLog(@"%@", strings);

...但我不清楚你需要完成什么。