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
)
究竟被認爲是一個分離器?一個沒有空格的逗號?這是正確的定義嗎? – 2013-05-10 09:55:21