2012-12-14 17 views

回答

1
NSString *input [email protected]"<one><two><three>"; 
NSString *strippedInput = [input stringByReplacingOccurencesOfString: @">" withString: @""]; //strips all > from input string 
NSArray *array = [strippedInput componentsSeperatedByString:@"<"]; 

注意,[陣列objectAtIndex:0]將是一個空串( 「」)的噸他當然不會工作,如果其中一個「實際」字符串包含<或>

+0

+1我在想同一個 –

+0

非常感謝,對不起,我在節假日休假時已經離線了。 – James

1

一種方法可能是使用兩種componentsSeparatedByCharactersInSet從NSString的componentsSeparatedByString

NSString *test = @"<one> <two> <three>"; 

NSArray *array1 = [test componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 

NSArray *array2 = [test componentsSeparatedByString:@"<"]; 

你需要做一些清理之後,無論是在數組2陣列1

2

東西的情況下,去除空白的字符串的情況下,微調證明:

NSString *string = @"<this is column 1><this is column 2>"; 
NSScanner *scanner = [NSScanner scannerWithString:string]; 

NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; 

NSString *temp; 

while ([scanner isAtEnd] == NO) 
{ 
    // Disregard the result of the scanner because it returns NO if the 
    // "up to" string is the first one it encounters. 
    // You should still have this in case there are other characters 
    // between the right and left angle brackets. 
    (void) [scanner scanUpToString:@"<" intoString:NULL]; 

    // Scan the left angle bracket to move the scanner location past it. 
    (void) [scanner scanString:@"<" intoString:NULL]; 

    // Attempt to get the string. 
    BOOL success = [scanner scanUpToString:@">" intoString:&temp]; 

    // Scan the right angle bracket to move the scanner location past it. 
    (void) [scanner scanString:@">" intoString:NULL]; 

    if (success == YES) 
    { 
     [array addObject:temp]; 
    } 
} 

NSLog(@"%@", array); 
相關問題