2012-01-15 84 views
3

我需要將短語拆分爲單詞,數字,標點符號和空格/製表符。我也想保留事物的順序。將文本拆分爲單詞,數字和標點符號

NSString *text = [NSString stringWithFormat:@"The 3 quick:\"brown fox, jump's\" over."]; 

這是我需要產生一種名單:

['The', ' ', '3', ' ', 'quick, ':', '"', 'brown', ' ', 'fox', ',', ' ', 'jump's', ' ', '.'] 

謝謝!!

+2

你從哪裏得到「quick」和「:」之間的空格? – 2012-01-15 13:42:57

+1

應該保留還是拆分全位數字符串?換句話說,「333快速」變成了「[」「」,「」,「333」,「」,「快速」],還是「[」「」,「3」,「3」 ,「3」,「」,「快」]'? – dasblinkenlight 2012-01-15 13:47:03

+0

應該保留數字。 「333」將保持333. – 2012-01-15 13:57:09

回答

2

試用這一類我寫了使用NSScanner & NSCharacterSet

@interface NSString(Splitting) 

-(NSArray *) arrayBySeparatingComponentsInCharacterSet:(NSCharacterSet *) charSet; 

@end 

@implementation NSString(Splitting) 

BOOL scanOneCharacterFromSetIntoString(NSScanner *self, NSCharacterSet * charSet, NSString **outStr); 
BOOL scanOneCharacterFromSetIntoString(NSScanner *self, NSCharacterSet * charSet, NSString **outStr) 
{ 
    // check for index out of bounds 
    NSString *inStr = self.string; 

    if (self.scanLocation >= inStr.length) 
    { 
     return NO; 
    } 

    unichar ch = [inStr characterAtIndex:self.scanLocation]; 

    if (![charSet characterIsMember:ch]) 
    { 
     return NO; 
    } 

    self.scanLocation++; 
    if (outStr) 
    { 
     *outStr = [NSString stringWithCharacters:&ch length:1]; 
    } 

    return YES; 
} 

-(NSArray *) arrayBySeparatingComponentsInCharacterSet:(NSCharacterSet *)charSet 
{ 
    NSScanner *scanner = [NSScanner scannerWithString:self]; 
    NSMutableArray *result = [NSMutableArray array]; 

    NSString *temp = nil; 
    while ([scanner scanUpToCharactersFromSet:charSet intoString:&temp] || scanOneCharacterFromSetIntoString(scanner, charSet, &temp)) {; 
     [result addObject:temp]; 

     if ([scanner scanLocation] >= [self length]) 
     { 
      break; 
     } 

     unichar temp2 = [self characterAtIndex:[scanner scanLocation]]; 

     if ([charSet characterIsMember:temp2]) 
     { 
      [result addObject:[NSString stringWithFormat:@"%c", temp2]]; 
      // only update the scan location if the scan was sucessful 
      [scanner setScanLocation:[scanner scanLocation] + 1]; 
     } 
    } 

    return result; 
} 

@end 

int main (int argc, const char * argv[]) 
{ 
    @autoreleasepool { 

     NSString *str = @"The 3 quick:\"brown fox, jump's\" over."; 
     NSArray *array = [str arrayBySeparatingComponentsInCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@" :\",'."]]; 
     NSLog(@"%@", array); 
    } 
} 

應該是你所需要的,只是改變字符集,你所需要的。還要注意,這是在啓用了ARC的情況下編譯的,所以它可能會或可能不會在引用計數環境中的內存管理中正常工作。

+0

謝謝!它奇妙地工作。你爲我節省了大量的挫折,更不用說時間了。 – 2012-01-15 14:18:23

+1

嘿,沒問題,只是樂意幫忙。 – 2012-01-15 14:25:35

+0

一個問題:NSString * str = @「hello world ...」; 句末有多個標點符號會導致崩潰。還有任何想法如何處理省略號(三個點「...」)? – 2012-01-18 15:29:01

相關問題