如何在两个单独的字符串中查找匹配的单词?

时间:2012-03-03 06:23:59

标签: iphone objective-c regex string nsstring

如果有两个单独的字符串,我们需要检索数组中匹配(在这些字符串中)的所有单词。此匹配也应不区分大小写。实现这一目标的最有效方法是什么。

例如:

NSString *s1 = @"Hello there, I want to match similar words.";  
NSString *s2 = @"Do you really want to match word, hello?";  

结果array应包含"hello", "want", "to", "match"

我经历了很多问题,并通过网络回复Regular expression to match a line that doesn't contain a word?,但没有找到解决方案。

4 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

您可以使用NSMutableSet进行此操作..

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

此解决方案适用于您,如果您的句子中没有任何标点符号。如果您想要使用带标点符号的句子,请删除句子中的所有标点符号。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

尝试使用以下代码

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