What is the most efficient way to count number of word in NSString without using regex?

前端 未结 7 1931
我在风中等你
我在风中等你 2020-12-20 05:31

I am a bit new to Objective C and was wondering if there is a better way to count words in a string.

ie:

NSString *str = @\"this is a string\";

// r         


        
相关标签:
7条回答
  • 2020-12-20 05:59

    In this situation, I'd use an NSScanner like so:

    NSString *str = @"this is a string";
    NSScanner *scanner = [NSScanner scannerWithString:str];
    NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSCharacterSet *nonWhitespace = [whiteSpace invertedSet];
    int wordcount = 0;
    
    while(![scanner isAtEnd])
    {
        [scanner scanUpToCharactersFromSet:nonWhitespace intoString:nil];
        [scanner scanUpToCharactersFromSet:whitespace intoString:nil];
        wordcount++;
    }
    

    This only creates two additional objects, no matter how long the string is.

    0 讨论(0)
  • 2020-12-20 05:59

    for storing string into an array

    NSArray *yourArray = [str componentsSeparatedByString:@" "];
    

    Update:

    and to count no of word you can use

    [yourArray count]
    
    0 讨论(0)
  • 2020-12-20 06:01

    Unless you're going to be doing it hundreds of times a second, I would just opt for the readable solution, something like the following pseudocode:

    def count (str):
        lastchar = " "
        count = 0
        for char as every character in string:
            if char is not whitespace and lastchar is whitespace:
                count = count + 1
            lastchar = char
        return count
    

    It seems a bit of a waste to create a whole array of other strings just so you can count them and throw them away.

    And if, for some reason, it becomes an issue, you can just replace the function body with a faster version. Make sure it is a problem first however. Optimisation of code that's fast enough already is wasted effort.

    0 讨论(0)
  • 2020-12-20 06:01

    One liner accurate solution:

    return [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]].count;
    
    0 讨论(0)
  • 2020-12-20 06:05

    This code will count the number of words (i.e., non-empty substrings) contained in a string that are separated by any number of space or line break characters:

    NSUInteger wordCount = 0;
    
    for (NSString* word in [someString
        componentsSeparatedByCharactersInSet:
        [NSMutableCharacterSet characterSetWithCharactersInString:@" \n"]]) {
    
        if (![word  isEqual: @""]) {
            wordCount++;
        }
    
    }
    

    It's a slight improvement to zoul's answer without recurring to regexes.

    0 讨论(0)
  • 2020-12-20 06:08

    There are two ways that don't involve collecting an array of words, and should be smarter than just breaking on spaces:

    • NSString's enumerateSubstringsInRange:options:usingBlock: method. Introduced to Cocoa Touch in iOS 4.0.
    • CFStringTokenizer.

    I would use one of these, even if I did want to collect or otherwise use the words.

    0 讨论(0)
提交回复
热议问题