拆分带有多个分隔符的NSString?

时间:2012-11-22 19:12:23

标签: objective-c cocoa nsstring

对于文本bozo__foo!!bar.baz,如何将包含此内容的NSString拆分为(bozo, foo, bar, baz)

也就是说,在包含字符串(分隔符)__!!.的组件中单独显示它。

5 个答案:

答案 0 :(得分:22)

您可以使用NSCharacterSet拆分字符串。试试这个

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

答案 1 :(得分: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;
}

示例:&#34; ABCD__EFGHI__JKLMNOP-QRST.UV_WXYZ&#34;

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

日志结果:&#34; (ABCD, EFGHI, JKLMNOP, QRST, "UV_WXYZ")&#34;

答案 2 :(得分:0)

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]);

答案 3 :(得分: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];
}

答案 4 :(得分:0)

我为项目解决了这个问题,方法是寻找最长的分隔符,用这个分隔符替换其他分隔符,然后在剩下的唯一一个分隔符上进行分隔。 试试这个:

  NSString *test = @"bozo__foo!!bar.baz";
  test = [test stringByReplacingOccurrencesOfString:@"!!" withString:@"__"];
  test = [test stringByReplacingOccurrencesOfString:@"." withString:@"__"];
  NSArray<NSString *> *parts = [test componentsSeparatedByString:@"__"];