【问题标题】:How to detect words within other words in Objective-C如何在 Objective-C 中检测其他单词中的单词
【发布时间】:2014-10-15 23:02:46
【问题描述】:

到目前为止,我可以检测到短语中的单词。例如,如果用户键入“pork”,则会返回一个名为“Cooked Pork”的项目。但是,如果用户输入“pork”,则不会返回“pork*”之类的项目。

这就是我比较数组并找出短语中单词的方式:

  for (NSString *allergicITem in matchAgainstArray)
{
    NSString *lowerString = [allergicITem lowercaseString];
    if ([theLowerCellString rangeOfString:lowerString].location != NSNotFound)
    {
        cell.textLabel.textColor = [UIColor redColor];
    }

    else
    {
        cell.textLabel.textColor = [UIColor blackColor];
    }
}

我的问题是如何解决这个问题,即使它在单词“pork*”中有一个像“*”这样的小扩展名,它也会检测到这个单词。

好吧,用非常简单的话来说,这就是我想要的:

如果一个数组中有“pork”这个词

另一个数组有工作“pork*”

我希望检测并返回单词“pork*”。

我的代码没有返回“pork*”,因此我问这个问题。

如果需要进一步说明,请告诉我。

更新:

我有两个由两个单独的数组填充的表视图。一个数组正在填充一个表格视图,其中填充了用户不喜欢的成分,另一个数组正在填充一个表格视图,其中填充了食品的成分。如果该食品的其中一种成分与某一种不受欢迎的食品相同,它将被标记为红色。

所以如果一种食物含有以下成分:

  • 鸡*

并且用户已经像这样填充了他的“不良成分”表视图:

  • 花生
  • 面筋

当配料表视图显示出来时,“chicken*”对象应该被标记为红色。

更新 2:

证明我的观点的代码(你们都可以测试): NSMutableArray *badIng = [[NSMutableArray alloc] init]; NSMutableArray *totalIng = [[NSMutableArray alloc] init];

[badIng addObject:@"Chicken"];
[badIng addObject:@"gluten"];

[totalIng addObject:@"Chicken*"];
[totalIng addObject:@"water"];


for (NSString *badItem in badIng)
{
    NSString *lowerbadString= [badItem lowercaseString];
    NSString *totalString = [totalIng objectAtIndex:0];
    NSString *totalLowerString = [totalString lowercaseString];

    if ([totalLowerString rangeOfString:lowerbadString].location != NSNotFound)
    {
        NSLog(@"Here is the object: %@", badItem);
    }

    else

    {
        NSLog(@"We didn't find it");
    }

}

【问题讨论】:

  • 您输入“熟猪肉”并搜索“猪肉”。 (注意空格。)那么“pork”不会在“porkupine”上受到打击(如果你允许我这个人为的例子)。
  • @JoshCaswell 为什么这个重复关闭? OP 已经在使用副本中的代码(大致)。显然,这个问题有一个特定的问题。简单地作为重复关闭并不能帮助 OP 解决他们的代码问题。
  • 如果lowerCellString 的值为@"pork*",那么[lowerCellString rangeOfString:@"pork"] 将返回(NSRange){.location=0, .length=4}。我可能错误地关闭了这个,但我不明白你在问什么。请澄清。
  • 那个“重复”不是。
  • 清楚地描述你的问题不仅是让别人理解的必要条件,也是你自己理解的必要条件。

标签: ios objective-c


【解决方案1】:

这样列出你的清单:

" cooked pork "
" roast beef "
" candied yams "
" porkupine "

(注意前导/尾随空格。)

然后使用" pork " 搜索上面的列表。 " cooked pork " 会匹配," porkupine " 不会。

【讨论】:

    【解决方案2】:

    哇,看来我找到了更好的方法来做到这一点。因为我使用的是 iOS 8,所以我这样做了:

    if ([theLowerCellString containsString:lowerString])
        {
            cell.textLabel.textColor = [UIColor redColor];
        }
    
        else
        {
            cell.textLabel.textColor = [UIColor blackColor];
        }
    

    现在它就像一个魅力一样工作,并返回我需要的对象。

    【讨论】:

    • 您发布的代码不显示您所说的问题。我在循环上方添加了一行以打印出这两项。这是控制台输出:2014-10-15 01:46:53.954 MatchWordsWithAsterisks[60753:303] Looking for 'Chicken' in 'chicken*' 2014-10-15 01:46:53.955 MatchWordsWithAsterisks[60753:303] Here is the object: Chicken 2014-10-15 01:46:53.956 MatchWordsWithAsterisks[60753:303] Looking for 'gluten' in 'chicken*' 2014-10-15 01:46:53.956 MatchWordsWithAsterisks[60753:303] We didn't find it 注意rangeOfString: found 'chicken' in 'chicken*'。
    猜你喜欢
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2018-10-24
    • 2015-11-19
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多