问题
If I have 2 strings: "My name is Eric" "America is great"
And my substring is 'eri'.
How can I write a function that will only return true for the first sentence, because it has a WORD that starts with eri (Eric) and does not just contain eri (AmERIca).
Note; I have been using NSPredicates, but using CONTAINS would return both, and using BEGINSWITH would only check the first word.
This string is also contained in an Object, say and the property is called , so my current code is:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF.name contains[cd] %@",
searchText];
searchResults = [[myArray filteredArrayUsingPredicate:resultPredicate] mutableCopy];
回答1:
Why not search for a string with leading space, like " eri"? That will automatically only find actual words and not something in the middle.
来源:https://stackoverflow.com/questions/11767936/find-occurrence-of-substring-in-sentence-only-if-word-starts-with-substring