How to count words within a text string?
问题 On iOS, how can I count words within a specific text string? 回答1: A more efficient method than splitting is to check the string character by character. int word_count(NSString* s) { CFCharacterSetRef alpha = CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric); CFStringInlineBuffer buf; CFIndex len = CFStringGetLength((CFStringRef)s); CFStringInitInlineBuffer((CFStringRef)s, &buf, CFRangeMake(0, len)); UniChar c; CFIndex i = 0; int word_count = 0; Boolean was_alpha = false, is_alpha;