Reputation: 83
I have 2 arrays which each of them contain different strings. I want to compare strings from those 2 and display matched strings only.
for instance, if words array has list of words = "wolf", "Wolfachite", "Wolfberry", "Wolf"
and if names array has list of words = "Winnie", "Wolf", "Wolfgang"
it would display "wolf".
sadly, I am confused which steps I must take to compare those two.
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
//Fill it into array
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
Upvotes: 0
Views: 543
Reputation: 5666
Use this code it compares your string
for (int i=0; i<[names count]; i++)
{
NSString *strNames = [[names objectAtIndex:i] uppercaseString];
for (int j=0; j<[words count]; j++)
{
NSString *strWords = [[words objectAtIndex:j] uppercaseString];
if ([strNames isEqualToString:strWords])
{
//your code when condition satisfied
}
}
}
Upvotes: 0
Reputation: 118671
A good option would be to use -[NSMutableSet intersectSet:]
but note that this compares the strings using isEqual:
— if you want a case-insensitive or diacritic-insensitive search then you will need to use your own custom objects and implement -isEqual:
and -hash
.
Upvotes: 1