问题
If there are two separate strings, we need to retrieve all words that are matching (in those strings), in an array. also this matching should be case insensitive. What will be the most efficient way to achieve this.
For Example:
NSString *s1 = @"Hello there, I want to match similar words.";
NSString *s2 = @"Do you really want to match word, hello?";
The resulting array should contain, "hello", "want", "to", "match".
I went through many questions and, responses over the net like Regular expression to match a line that doesn't contain a word?, but didn't, find solution needed.
回答1:
You can use NSMutableSet for this..
NSString *s1 = @"Hello there, I want to match similar words.";
NSString *s2 = @"Do you really want to match word, hello?";
NSArray *components = [s1 componentsSeparatedByString:@" "];
NSArray *components1 = [s2 componentsSeparatedByString:@" "];
NSLog(@"%@",components);
NSLog(@"%@",components1);
NSMutableSet *resultSet = [NSMutableSet setWithArray:components1];
NSSet *setA = [NSSet setWithArray:components];
[resultSet intersectSet:setA];
NSArray *result = [resultSet allObjects];
NSLog(@"%@",result);
This Solution will work for you, If you don't have any punctuations in the sentences.. If you want sentences with punctuations to work just remove all the punctuations in your sentences.
回答2:
try this:
NSString *s1 = @"Hello there, I want to match similar words.";
NSString *s2 = @"Do you really Want to match word, hello?";
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" ?.,"];
NSArray *as1 = [[s1 lowercaseString] componentsSeparatedByCharactersInSet:separatorSet];
NSArray *as2 = [[s2 lowercaseString] componentsSeparatedByCharactersInSet:separatorSet];
NSMutableSet* set1 = [NSMutableSet setWithArray:as1];
NSMutableSet* set2 = [NSMutableSet setWithArray:as2];
[set1 intersectSet:set2];
[set1 removeObject:@""];
NSArray* result =[set1 allObjects];
NSLog(@"%@",result);
回答3:
NSString *s1 = @"Hello there, I want to match similar words.";
NSString *s2 = @"Do you really want to match word, hello?";
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" ?.,"];
NSArray *s1Array = [s1 componentsSeparatedByCharactersInSet:separatorSet];
NSArray *s2Array = [s2 componentsSeparatedByCharactersInSet:separatorSet];
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:0];
for(int index = 0; index < [s1Array count]; index++){
NSString *compareString = [[s1Array objectAtIndex:index] lowercaseString];
NSUInteger findIndex = [s2Array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
NSString *currentString = (NSString *)obj;
if([[currentString lowercaseString] isEqualToString:compareString] &&
![currentString isEqualToString:@""]){
return YES;
*stop = YES;
}else{
return NO;
}
}
];
if(findIndex !=NSNotFound){
[resultArray addObject:compareString];
}
}
NSLog(@"Result:%@",[resultArray description]);
回答4:
Try with below code
NSString *s1 = @"Hello there, I want to match similar words.";
NSString *s2 = @"Do you really want to match word, hello?";
NSCharacterSet *doNotWant = [[NSCharacterSet letterCharacterSet] invertedSet];
NSArray *array1 = [[s1 uppercaseString] componentsSeparatedByCharactersInSet:doNotWant];
NSArray *array2 = [[s2 uppercaseString] componentsSeparatedByCharactersInSet:doNotWant];
NSSet *set1 = [[NSSet alloc] initWithArray:array1];
NSMutableSet *set2 = [[NSMutableSet alloc] initWithArray:array2];
[set2 intersectSet:set1];
NSLog(@"set2:%@",set2);
NSLog(@"set1:%@",set1);
NSArray *aray_res = [set2 allObjects];
NSLog(@"aray_res:%@",aray_res);
来源:https://stackoverflow.com/questions/9544040/how-to-find-matching-words-in-two-separate-strings