Find occurrence of substring in sentence only if word starts with substring

亡梦爱人 提交于 2019-12-08 06:46:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!