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..!!!

Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • 2
    `myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];` – Lithu T.V Jun 27 '13 at 12:27
  • this is a perfect ans... – Mike Jun 27 '13 at 12:34
  • http://stackoverflow.com/questions/6171422/objective-c-nsstring-wordcount http://stackoverflow.com/questions/4724206/how-do-you-get-the-number-of-words-in-a-nstextstorage-nsstring http://stackoverflow.com/questions/3975209/what-is-the-most-efficient-way-to-count-number-of-word-in-nsstring-without-using Alot of duplicates. – Jens Bergvall Jun 27 '13 at 12:44

6 Answers6

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
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
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];
}
Karl
  • 1,233
  • 8
  • 16
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++;
}
Monolo
  • 18,205
  • 17
  • 69
  • 103
Mike
  • 737
  • 1
  • 6
  • 14
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);
iSmita
  • 1,292
  • 1
  • 9
  • 24
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];
    }
Trup
  • 635
  • 5
  • 17