2013-06-27 25 views
5

मैं शब्दमैं टेक्स्ट व्यू में शब्दों और चरित्र की कुल संख्या की गणना कैसे कर सकता हूं?

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

    } 

लेकिन जब मैं से अधिक दर्ज सही गिनती नहीं हो रही im के लिए कोड है दो लाइनें यह शब्द के रूप में नई लाइन लेता है ..

कृपया मदद करें !!! !!!

+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- ए-nstextstorage-nsstring http://stackoverflow.com/questions/3975209/what-is-the-most- कुशल-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

तुम सच में शब्द गिनती करने के लिए चाहते हैं (यानी "foo, बार" 2 शब्द रूप में गिना जाए 6 वर्णों के साथ) आप 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]); 
संबंधित मुद्दे