How to get the first N words from a NSString in Objective-C?

后端 未结 4 1408
无人及你
无人及你 2020-12-08 06:14

What\'s the simplest way, given a string:

NSString *str = @\"Some really really long string is here and I just want the first 10 words, for example\";
         


        
4条回答
  •  情话喂你
    2020-12-08 06:49

    While Barry Wark's code works well for English, it is not the preferred way to detect word breaks. Many languages, such as Chinese and Japanese, do not separate words using spaces. And German, for example, has many compounds that are difficult to separate correctly.

    What you want to use is CFStringTokenizer:

    CFStringRef string; // Get string from somewhere
    CFLocaleRef locale = CFLocaleCopyCurrent();
    
    CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, string, CFRangeMake(0, CFStringGetLength(string)), kCFStringTokenizerUnitWord, locale);
    
    CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
    unsigned tokensFound = 0, desiredTokens = 10; // or the desired number of tokens
    
    while(kCFStringTokenizerTokenNone != (tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer)) && tokensFound < desiredTokens) {
      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);
    

提交回复
热议问题