How to find matching words in two separate strings?

断了今生、忘了曾经 提交于 2019-12-04 21:28:38

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.

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

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!