Ios 如何计算textView中的单词和字符总数?
我使用下面的代码来计算单词总数Ios 如何计算textView中的单词和字符总数?,ios,iphone,objective-c,xcode,ios7,Ios,Iphone,Objective C,Xcode,Ios7,我使用下面的代码来计算单词总数 -(NSInteger) getTotalWords{ NSLog(@"Total Word %lu",[[_editor.attributedText string]length]); if ([[_editor.attributedText string]length]==0) { return 0; } NSString *str =[_editor textInRange:[_editor textRan
-(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;
}
但是当我输入超过两行时,我并没有得到完美的计数。它以新行作为单词
请帮帮我 对于字数计算
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++;
}
你可以简单地做:
NSString *text = @"Lorem ...";
NSArray *words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSInteger wordCount = [words count];
NSInteger characterCount = 0;
for (NSString *word in words) {
characterCount += [word length];
}
如果你真的想计算单词数(即“foo,bar”应计算为2个单词,并加上 6个字符)然后 您可以使用
EnumerateStringsRange
的NSStringEnumerationByWords
选项,
自动处理所有空格和分词符:
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
要获得字数,请使用-
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);
NSArray*array=[txtView.text组件由字符串分隔:@”“;
int wordCount=[array count];
对于(int i=0;i<[数组计数];i++){
characterCount=characterCount+[[array objectAtIndex:i]length];
}
NSLog(@“characterCount:%i”,characterCount);
NSLog(@“字数:%i”,字数);
str=textView.text;
NSArray*wordArray=[str componentsSeparatedByString:@'];
int wordCount=[wordArray count];
int charCount=0;
对于(int i=0;i<[wordArray count];i++)
{
charCount=charCount+[[wordArray objectAtIndex:0]长度];
}
像这样尝试一次
NSString *string = @"123 1 2\n234\nponies";
NSArray *chunks = [string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n"]];
NSLog(@"%d",[chunks count]);
myString=[mystringstringbyreplacingoccurrencesofstring:@“\n”with string:@”“代码>这是一个完美的答案。。。有很多副本。谢谢。。这对我来说很管用,不过这也算是个词。
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];
}
NSString *string = @"123 1 2\n234\nponies";
NSArray *chunks = [string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n"]];
NSLog(@"%d",[chunks count]);