2013-06-27 45 views
5

我使用下面的代碼來計算的話如何計算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; 
} 

這裏的總數是總字符

-(NSInteger) getTotalChars{ 

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

     NSLog(@"%@",str); 

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

     return charCount=charCount-1; 

    } 

但是,當我輸入超過我不是得到完美的計數的代碼兩條線。它需要新的一行作爲字..

請幫助.. !!!

+2

'的myString = [myString的stringByReplacingOccurrencesOfString:@ 「\ n」 withString :@「」];' –

+0

這是一個完美的答案... – 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 很多重複。 – doge

回答

4

你可以簡單地做:

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

這個數字雖然是單詞。 – Supertecnoboff

0

詞數...

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

別忘了+1 – mindfreak

9

如果你真的想算(即 「富,酒吧」 應該算作2個字與 6個字符),然後 您可以使用NSStringEnumerationByWords選項enumerateSubstringsInRange, 它自動處理所有的空格和單詞分隔符:

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; 

爲了得到字數使用 -

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

一次嘗試這樣的,

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