我有两个逗号分隔NSString
和&我想从第一个字符串中删除相似的字符。
ex. str1 = 0,1,2,3
str2 = 1,2.
output -> str1 = 0,3 and str2 = 1,2.
我有一个选项,用数组中的逗号分隔值分隔两个字符串。但它需要两个NSArray
&应用loop
然后删除常用元素,但这是非常繁琐的工作。
所以我想要一些简单的&避免循环的适当解决方案。
请帮助我解决这个问题。
答案 0 :(得分:6)
试试这个:
不需要循环!!!
您已获得所有必需的API。
NSString *str1=@"0,1,2,3";
NSString *str2=@"1,2";
NSMutableArray *arr1=[[NSMutableArray alloc]initWithArray:[str1 componentsSeparatedByString:@","]];
[arr1 removeObjectsInArray:[str2 componentsSeparatedByString:@","]];
NSLog(@"arr1 %@",arr1);
/*
NSMutableString *finalString=[NSMutableString new];
for (NSInteger i=0; i<[arr1 count]; i++) {
NSString *str=[arr1 objectAtIndex:i];
[finalString appendString:str];
if (i!=[arr1 count]-1) {
[finalString appendString:@","];
}
}
*/
NSString *finalString=[arr1 componentsJoinedByString:@","];
NSLog(@"finalString %@",finalString);
答案 1 :(得分:0)
那样的东西?
NSString *string = @"0,1,2,3";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self like '1' OR self like '2'"];
NSLog(@"%@",[[string componentsSeparatedByString:@","] filteredArrayUsingPredicate:predicate]);
答案 2 :(得分:0)
id str1=@"aa,ab,ac,cd,ce,cf";
id str2=@"aa,ac,cd,cf";
//no ab and no ce
id cmps1 = [str1 componentsSeparatedByString:@","];
id cmps2 = [str2 componentsSeparatedByString:@","];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT SELF IN %@", cmps2];
NSArray *final = [cmps1 filteredArrayUsingPredicate:predicate];
id str = [final componentsJoinedByString:@","];
NSLog(@"%@", str);