【问题标题】:split NSString using componentsSeparatedByString使用 componentsSeparatedByString 拆分 NSString
【发布时间】:2013-05-04 23:25:09
【问题描述】:

我有一个字符串需要拆分。使用 componentsSeparatedByString 会很容易,但我的问题是分隔符是逗号,但我可以使用不是分隔符的逗号。

我解释一下:

我的字符串:

NSString *str = @"black,red, blue,yellow";

红色和蓝色之间的逗号不能作为分隔符。

我可以确定逗号是分隔符还是不检查逗号后面是否有空格。

目标是获得一个数组:

(
black,
"red, blue",
yellow
)

【问题讨论】:

  • 究竟什么是分隔符?逗号后面没有空格?这是正确的定义吗?

标签: objective-c nsstring foundation


【解决方案1】:

这很棘手。首先用say '|'替换所有出现的', '(逗号+空格)然后使用组件分离的方法。完成后,再次替换'|'使用 ', '(逗号+空格)。

【讨论】:

  • 我在这个方向上使用 NSRegularExpression 然后使用 stringByReplacingMatchesInString
【解决方案2】:

只是为了完成图片,一个使用正则表达式来直接识别逗号而不是空格的解决方案,正如您在问题中所解释的那样。

正如其他人建议的那样,使用此模式替换为临时分隔符字符串并以此分割。

NSString *pattern = @",(?!\\s)"; // Match a comma not followed by white space.
NSString *tempSeparator = @"SomeTempSeparatorString"; // You can also just use "|", as long as you are sure it is not in your input.

// Now replace the single commas but not the ones you want to keep
NSString *cleanedStr = [str stringByReplacingOccurrencesOfString: pattern
                                                      withString: tempSeparator
                                                         options: NSRegularExpressionSearch
                                                           range: NSMakeRange(0, str.length)];

// Now all that is needed is to split the string
NSArray *result = [cleanedStr componentsSeparatedByString: tempSeparator];   

如果您不熟悉使用的正则表达式模式,(?!\\s) 是一个否定的前瞻,您可以找到很好的解释,例如here

【讨论】:

  • 最佳答案!唯一不需要循环的解决方案。在我的例子中,我在想要保留的逗号之前有一个转义字符“\”,所以就在调用componentsSeparatedByString 之前,我删除了转义字符
【解决方案3】:

这是 cronyneaus4u 解决方案的编码实现:

NSString *str = @"black,red, blue,yellow";
str = [str stringByReplacingOccurrencesOfString:@", " withString:@"|"];
NSArray *wordArray = [str componentsSeparatedByString:@","];
NSMutableArray *finalArray = [NSMutableArray array];
for (NSString *str in wordArray)
{
    str = [str stringByReplacingOccurrencesOfString:@"|" withString:@", "];
    [finalArray addObject:str];
}
NSLog(@"finalArray = %@", finalArray);

【讨论】:

    【解决方案4】:
    NSString *str = @"black,red, blue,yellow";
    NSArray *array = [str componentsSeparatedByString:@","];
    NSMutableArray *finalArray = [[NSMutableArray alloc] init];
    for (int i=0; i < [array count]; i++) {
        NSString *str1 = [array objectAtIndex:i];
        if ([[str1 substringToIndex:1] isEqualToString:@" "]) {
            NSString *str2 = [finalArray objectAtIndex:(i-1)];
            str2 = [NSString stringWithFormat:@"%@,%@",str2,str1];
            [finalArray replaceObjectAtIndex:(i-1) withObject:str2];
        }
        else {
            [finalArray addObject:str1];
        }
    }
    
    NSLog(@"final array count : %d description : %@",[finalArray count],[finalArray description]);
    

    输出:

    final array count : 3 description : (
    black,
    "red, blue",
    yellow
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      相关资源
      最近更新 更多