2013-06-27 27 views
5

używam następujący kod do obliczenia całkowitej liczby słówJak mogę obliczyć całkowitą liczbę słów i znaków w textView?

-(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; 
} 

i tu jest kod na łączną charakteru

-(NSInteger) getTotalChars{ 

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

     NSLog(@"%@",str); 

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

     return charCount=charCount-1; 

    } 

ale im nie otrzymania idealnego count kiedy wprowadzić więcej niż dwie linie. nowa linia to słowo ...

prosze o pomoc .. !!!

+2

'myString = [myString stringByReplacingOccurrencesOfString: @ "\ n" withString : @ ""]; ' –

+0

to jest idealne ans ... – mindfreak

+0

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 Dużo duplikaty. – doge

Odpowiedz

4

Można po prostu zrobić:

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

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

NSInteger characterCount = 0; 
for (NSString *word in words) { 
    characterCount += [word length]; 
} 
+0

To się liczy "" jak słowa. – Supertecnoboff

0

dla liczby słów ...

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

Dzięki .. jego prace dla mnie .. –

+0

nie zapomnij +1 – mindfreak

9

Jeśli naprawdę chcesz liczyć słowa (czyli "foo, bar" należy liczyć jako 2 słowach z 6 znaków), a następnie możesz użyć opcji NSStringEnumerationByWords z enumerateSubstringsInRange, , która automatycznie obsługuje wszystkie białe spacje i separatory słów:

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 
0
int characterCount = 0; 

Uzyskania korzystania liczba słów -

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]; 
    } 
1

raz spróbować tak,

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