5

i am using the following code to calculate the total number of words

-(NSInteger) getTotalWords{
    NSLog(@"Total Word %lu",[[_editor.attributedText string]length]);
    if ([[_editor.attributedText string]length]==0) {
        return 0;
    }

    NSString *str  =[_editor textInRange:[_editor textRangeWithRange:[self visibleRangeOfTextView:_editor]]];

    NSInteger sepWord = [[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsSeparatedByString:@" "] count];
    sepWord += [[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsSeparatedByString:@"\n"] count];
    sepWord=sepWord-2;
    return sepWord;
}

and here is the code for the total character

 -(NSInteger) getTotalChars{

        NSString *str  =[_editor textInRange:[_editor textRangeWithRange:[self visibleRangeOfTextView:_editor]]];

        NSLog(@"%@",str);

        NSInteger charCount= [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]length];

        return charCount=charCount-1;

    }

But i m not getting the perfect count when i enter more than two lines. it takes new line as word..

please help..!!!

3

6 Answers 6

9

If you really want to count words (i.e. "foo,bar" should count as 2 words with 6 characters) then you can use the NSStringEnumerationByWords option of enumerateSubstringsInRange, which handles all the white space and word separators automatically:

NSString *string = @"Hello world.\nfoo,bar.";

__block int wordCount = 0;
__block int charCount = 0;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
               options:NSStringEnumerationByWords
            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                wordCount += 1;
                charCount += substringRange.length;
            }];
NSLog(@"%d", wordCount); // Output: 4
NSLog(@"%d", charCount); // Output: 16
4

You could simply do:

NSString *text = @"Lorem ...";

NSArray *words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSInteger wordCount = [words count];

NSInteger characterCount = 0;
for (NSString *word in words) {
   characterCount += [word length];
}
1
  • This counts " " as words though. Feb 19, 2016 at 11:49
1

once try like this,

 NSString *string = @"123 1 2\n234\nponies";
    NSArray *chunks = [string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n"]];
    NSLog(@"%d",[chunks count]);
0

for word count...

NSString *str = @"this is a sample 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++;
}
0
0
int characterCount = 0;

For getting word count use -

NSArray *array = [txtView.text componentsSeparatedByString:@" "];
int wordCount = [array count];

for(int i = 0; i < [array count]; i++){
    characterCount = characterCount + [[array objectAtIndex:i] length];
}

NSLog(@"characterCount : %i",characterCount);
NSLog(@"wordCount : %i",wordCount);
0
    str = textView.text;

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

    int wordCount = [wordArray count];
    int charCount=0;

    for (int i=0 ; i < [wordArray count]; i++)
    {
        charCount = charCount + [[wordArray objectAtIndex:0] length];
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.