Detecting if an NSString contains…?

后端 未结 7 2215
长情又很酷
长情又很酷 2021-01-30 03:37

How can I detect if a string contains a certain word? For example, I have a string below which reads:

@\"Here is my string.\"

I\'d like to know

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 04:04

    I recommend using NSLinguisticTagger. We can use it to search Here is my string. His isn't a mississippi isthmus. It is?

    NSLinguisticTagger *linguisticTagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[
                                            NSLinguisticTagSchemeTokenType,
                                            ]
                                                                                  options:
                                            NSLinguisticTaggerOmitPunctuation |
                                            NSLinguisticTaggerOmitWhitespace |
                                            NSLinguisticTaggerOmitOther ];
    [linguisticTagger setString:@"Here is my string. His isn't a mississippi isthmus. It is?"];
    [linguisticTagger enumerateTagsInRange:NSMakeRange(0,
                                                       [[linguisticTagger string] length])
                                    scheme:NSLinguisticTagSchemeTokenType
                                   options:
     NSLinguisticTaggerOmitPunctuation |
     NSLinguisticTaggerOmitWhitespace |
     NSLinguisticTaggerOmitOther |
     NSLinguisticTaggerJoinNames
                                usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                                    NSLog(@"tag: %@, tokenRange: %@, sentenceRange: %@, token: %@",
                                          tag,
                                          NSStringFromRange(tokenRange),
                                          NSStringFromRange(sentenceRange),
                                          [[linguisticTagger string] substringWithRange:tokenRange]);
                                }];
    

    This outputs:

    tag: Word, tokenRange: {0, 4}, sentenceRange: {0, 19}, token: Here
    tag: Word, tokenRange: {5, 2}, sentenceRange: {0, 19}, token: is
    tag: Word, tokenRange: {8, 2}, sentenceRange: {0, 19}, token: my
    tag: Word, tokenRange: {11, 6}, sentenceRange: {0, 19}, token: string
    tag: Word, tokenRange: {19, 3}, sentenceRange: {19, 33}, token: His
    tag: Word, tokenRange: {23, 2}, sentenceRange: {19, 33}, token: is
    tag: Word, tokenRange: {25, 3}, sentenceRange: {19, 33}, token: n't
    tag: Word, tokenRange: {29, 1}, sentenceRange: {19, 33}, token: a
    tag: Word, tokenRange: {31, 11}, sentenceRange: {19, 33}, token: mississippi
    tag: Word, tokenRange: {43, 7}, sentenceRange: {19, 33}, token: isthmus
    tag: Word, tokenRange: {52, 2}, sentenceRange: {52, 6}, token: It
    tag: Word, tokenRange: {55, 2}, sentenceRange: {52, 6}, token: is
    

    It ignores His mississippi and isthmus and even identifies is inside of isn't.

提交回复
热议问题