代码之家  ›  专栏  ›  技术社区  ›  RunLoop

用于搜索字符串中每个单词的第一个字母的谓词

  •  0
  • RunLoop  · 技术社区  · 14 年前

    我想创建一个谓词来搜索字符串中每个单词开头的一个特定字母,例如,所有以in开头的单词@“The man at e apples”,都将返回ate和apples。可以创建这样的谓词吗?谢谢您。

    1 回复  |  直到 9 年前
        1
  •  2
  •   mfazekas    9 年前

    nspredicate在计算时返回true或false,因此不能创建返回以“a”开头的所有单词的谓词。

    您可以创建一个谓词,该谓词对于以“a”开头的单词返回true,然后将句子拆分到world,并使用谓词筛选好的单词,例如:

    NSArray *words = [sentence componentsJoinedByString:@" "];
    NSPredicate *beginsWithA = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];
    NSArray *wordsBeginsWithA = [words filteredArrayUsingPredicate:beginsWithA];