How to get an array of sentences using CFStringTokenizer?

∥☆過路亽.° 提交于 2019-11-28 23:52:47
sbooth

Here is an example of CFStringTokenizer usage:

CFStringRef string; // Get string from somewhere
CFLocaleRef locale = CFLocaleCopyCurrent();

CFStringTokenizerRef tokenizer = 
    CFStringTokenizerCreate(
        kCFAllocatorDefault
        , string
        , CFRangeMake(0, CFStringGetLength(string))
        , kCFStringTokenizerUnitSentence
        , locale);

CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
unsigned tokensFound = 0;

while(kCFStringTokenizerTokenNone !=
    (tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer))) {
    CFRange tokenRange = CFStringTokenizerGetCurrentTokenRange(tokenizer);
    CFStringRef tokenValue =
        CFStringCreateWithSubstring(
            kCFAllocatorDefault
            , string
            , tokenRange);

  // Do something with the token
  CFShow(tokenValue);
  CFRelease(tokenValue);
  ++tokensFound;
}

// Clean up
CFRelease(tokenizer);
CFRelease(locale);

You may also use:

    [mutstri enumerateSubstringsInRange:NSMakeRange(0, [mutstri length])
                                options:NSStringEnumerationBySentences
                             usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                                 NSLog(@"%@", substring);

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