2012-11-22 98 views
7

對於文本bozo__foo!!bar.baz,如何將包含這個的NSString拆分爲(bozo, foo, bar, baz)拆分NSString與多個分隔符?

也就是說,將其與具有字符串(分隔符)__,!!.的組件分開。

回答

21

您可以使用NSCharacterSet分割字符串。試試這個

NSString *[email protected]"bozo__foo!!bar.baz"; 
NSString *sep = @"_!."; 
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep]; 
NSArray *temp=[test componentsSeparatedByCharactersInSet:set]; 
NSLog(@"temp=%@",temp); 
+0

如果'sep'不是一個字符,就可以工作。例如如果我想用'!!'而不是'!'來分割 – Arefly

1
NSString *text = @"bozo__foo!!bar.baz"; 

NSArray *split1 = [text componentsSeparatedByString:@"__"]; 
NSArray *split2 = [[split1 lastObject] componentsSeparatedByString:@"!!"]; 
NSArray *split3 = [[split2 lastObject] componentsSeparatedByString:@"."]; 

NSLog(@"%@, %@, %@, %@", split1[0], split2[0], split3[0], split3[1]); 
+1

作爲回答,如果這些分裂沒有下令將無法正常工作,但因爲在我的情況是這樣,那麼我將其標記。 – Matoe

1

我知道這個問題已經被回答了,但這是一種使用多個字符串分隔字符串的方法。這是一個類別到NSString。

- (NSArray<NSString *> *)componentsSeparatedByStrings:(NSArray<NSString *> *)separators 
{ 
    NSMutableArray<NSString *> *components = [[NSMutableArray alloc] init]; 
    unichar buffer[self.length + 1]; 
    NSInteger currentOrigin = 0; 
    NSInteger currentLength = 0; 

    [self getCharacters:buffer]; 

    for(NSInteger i = 0; i < self.length; i++) 
    { 
     unichar currentChar = buffer[i]; 

     currentLength++; 

     for(NSInteger n = 0; n < separators.count; n++) 
     { 
      NSString *currentDivider = [separators objectAtIndex:n]; 

      if(currentDivider.length == 0) 
      { 
       return @[self]; 
      } 
      else if(currentDivider.length > 1) 
      { 
       BOOL goodMatch = NO; 

       for(NSInteger x = 0; x < currentDivider.length; x++) 
       { 
        unichar charInDivider = [currentDivider characterAtIndex:x]; 

        if(charInDivider == currentChar) 
        { 
         goodMatch = YES; 
        } 
        else 
        { 
         goodMatch = NO; 
         break; 
        } 

        if(goodMatch == YES && ((x + 1) != currentDivider.length)) 
        { 
         i++; 
         currentLength++; 

         currentChar = buffer[i]; 
        } 
       } 

       if(goodMatch == YES) 
       { 
        NSRange newComponentRange = NSMakeRange(currentOrigin, (currentLength - currentDivider.length)); 
        NSString *newComponent = [self substringWithRange:newComponentRange]; 
        currentOrigin = (i + 1); 
        currentLength = 0; 

        [components addObject:newComponent]; 
        NSLog(@"%@", components); 
       } 
      } 
      else // If current divider is only one character long. 
      { 
       if([currentDivider characterAtIndex:0] == currentChar) 
       { 
        NSRange newComponentRange = NSMakeRange(currentOrigin, (currentLength - 1)); 
        NSString *newComponent = [self substringWithRange:newComponentRange]; 
        currentOrigin = (i + 1); 
        currentLength = 0; 

        [components addObject:newComponent]; 
        break; 
       } 
      } 
     } 

     // Handle the end of the string. 
     if((i + 1) == self.length) 
     { 
      NSRange newComponentRange = NSMakeRange(currentOrigin, currentLength); 
      NSString *newComponent = [self substringWithRange:newComponentRange]; 
      currentOrigin = 0; 
      currentLength = 0; 

      [components addObject:newComponent]; 
     } 
    } 

    return components; 
} 

示例: 「ABCD__EFGHI__JKLMNOP-QRST.UV_WXYZ」

NSLog(@"%@", [test componentsSeparatedByStrings:@[@"__", @"-", @"."]]); 

登錄結果:(ABCD, EFGHI, JKLMNOP, QRST, "UV_WXYZ")

0

更多官能解決方案是應用-componentsSeparatedByString:遞歸,對於每個部件,這是在以前的分離器應用中得到的:

的NSString類別

- (NSMutableArray<NSString *> *)gvr_componentsSeparatedByStrings:(NSArray<NSString *> *)separators { 
    if (separators.count == 0) { 
     return [NSMutableArray arrayWithObject:self]; 
    } 

    NSString *separator = [separators firstObject]; 

    NSArray *reducedSeparators = [separators gvr_arrayByRemovingFirstObject]; 

    NSArray *components = [self componentsSeparatedByString:separator]; 

    NSMutableArray *result = [NSMutableArray new]; 

    for (NSString *component in components) { 
     NSMutableArray *subResult = [component gvr_componentsSeparatedByStrings:reducedSeparators]; 

     [result addObjectsFromArray:subResult]; 
    } 

    return result; 
} 

NSArray的範疇

- (NSArray *)gvr_arrayByRemovingFirstObject { 
    NSMutableArray *result = [NSMutableArray new]; 
    for (NSInteger i = 1; i < self.count; i++) { 
     [result addObject:self[i]]; 
    } 

    return [result copy]; 
}