NSStrings - Basic Term Extraction [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this questionI'm trying to perform basic text extraction on two NSString instances.
What is the best way to extract the list of words that are common in both strings?
E.g. if I have "It is a sunny day today" and "Today is sunny as it is summer" then the result should be an array containing "it, is, sunny, today"
Fill 2 arrays with the terms you'll find in each array, then make a loop on one array to see if the term is present in the other one. You can improve the loop by sorting them first and stopping the search earlier.
This should help you.
NSMutableArray *arrCommonWords =[[NSMutableArray alloc] init];
NSString *stringWithWOrds1;
NSArray *stringArray1 = [stringWithWOrds componentsSeparatedByString:@" "]; //Here put your sepqrator (I have put space)
NSString *stringWithWOrds2;
NSArray *stringArray2 = [stringWithWOrds componentsSeparatedByString:@" "]; //Here put your sepqrator (I have put space)
for(NSString *strTmp in stringArray1)
{
for(NSString *strTmp1 in stringArray2)
{
if([strTmp isEqualToString:strTmp1])
{
[arrCommonWords addObject:strTmp];
break;
}
}
}
精彩评论