[英]How to find matching words in two separate strings?
如果有兩個單獨的字符串,則需要檢索數組中所有匹配的單詞(在這些字符串中)。 而且此匹配應該不區分大小寫。 實現這一目標的最有效方法是什么。
例如:
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"
。
我經歷了許多問題,並且像正則表達式一樣通過網絡進行響應, 以匹配不包含單詞的行? ,但沒有找到所需的解決方案。
嘗試這個:
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);
您可以為此使用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);
如果您的句子中沒有標點符號,則此解決方案將為您工作。如果您希望帶標點符號的句子有效,只需刪除句子中的所有標點符號即可。
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]);
嘗試以下代碼
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.