2012-03-03 208 views
0

如果有兩個單獨的字符串,我們需要在數組中檢索所有匹配的單詞(在這些字符串中)。這個匹配也應該不區分大小寫。什麼是最有效的方式來實現這一點。如何在兩個單獨的字符串中查找匹配的單詞?

例如:

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?許多問題,並響應去了,但沒有,找到解決方案所需。

回答

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

本解決方案會爲你工作,如果你沒有在句子的任何標點符號。如果你想用標點句工作只是刪除所有句子中的標點符號。

+0

不會工作,因爲@「你好」和@「你好」是不相等的,因此他們不會在結果陣列。 – 2012-03-03 07:27:40

+0

坦克@Aravindhanarvi對於這樣的擔憂和快速反應,在添加小邏輯後效果很好。因爲大寫字母在比較之前已被轉換成小寫字母。 – rptwsthi 2012-03-06 08:24:34

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

你的反應非常棒,實際上這是我達到的目標,但是Aravindhanarvi領先一步,如你所見。非常感謝你,這個答案會對很多人有幫助,我相信。再次感謝。 – rptwsthi 2012-03-06 08:20:04

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

+1謝謝你指出Aravindhanarvi的邏輯缺陷,幫助改進代碼。 – rptwsthi 2012-03-06 08:25:56

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); 
相關問題