仅当单词以子字符串开头时,才在句子中查找子字符串的出现 [英] Find occurrence of substring in sentence only if word starts with substring

查看:53
本文介绍了仅当单词以子字符串开头时,才在句子中查找子字符串的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有2个字符串: 我的名字叫埃里克" 美国很棒"

If I have 2 strings: "My name is Eric" "America is great"

我的子字符串是'eri'.

And my substring is 'eri'.

我该如何编写一个只对第一句话返回true的函数,因为它有一个以eri(Eric)开头的单词,而不仅仅是eri(AmERIca).

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).

注意; 我一直在使用NSPredicates,但是使用CONTAINS会返回两个值,而使用BEGINSWITH只会检查第一个单词.

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];

推荐答案

为什么不搜索带有前导空格的字符串,例如"eri"?这样只会自动找到实际的单词,而不会找到中间的单词.

Why not search for a string with leading space, like " eri"? That will automatically only find actual words and not something in the middle.

这篇关于仅当单词以子字符串开头时,才在句子中查找子字符串的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆