Re: Word count
Re: Word count
- Subject: Re: Word count
- From: Zach Wily <email@hidden>
- Date: Wed, 9 Jun 2004 21:50:31 -0600
(Typed in Mail...)
int words = 0;
NSScanner *scanner = [NSScanner scannerWithString:string];
while (![scanner isAtEnd])
{
[scanner scanCharactersFromSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet] intoString:nil];
if ([scanner scanCharactersFromSet:[[NSCharacterSet
whitespaceAndNewlineCharacterSet] invertedSet] intoString:nil])
words++;
}
zach
On Jun 9, 2004, at 9:26 PM, Matt Baker wrote:
I've seen the NSScanner examples, but am unable to determine how to
use it to count words. Your previous example of counting the parts of
an NSarray worked unless I had two spaces, like "two words" would
count as 3 words with the following:
int wc = [[[myAttributedString string] componentsSeparatedByString: @"
"] count];
I suppose I could sort the array, then count a sub-array that has no
empty elements in it, but talk about non-elegant!
So, for anyone who's run into counting / parsing, what's the quickest
way to accurately count words with NSScanner (or anything else in
Cocoa)?
Thanks,
Matt
j o a r wrote:
To avoid having to create a lot of temporary string objects (as with
my previous suggestion), you might also consider using an NSScanner
to run a pass over the string and count spaces (or other whitespace
characters - use the whitespace character set). It might be more
efficient - test it to find out.
j o a r
On 2004-06-09, at 08.05, Matt Baker wrote:
Anyone know an effiecient way to do a word count on an NSString or
(better yet) an NSAttributedString? I'm having trouble getting the
spell checker to count the words (always returns 0) and I see I can
get an array of the words using NSTextStorage, but that horribly
memory-sucking in my opinion. That, and I cannot instantiate a
NSTextStorage object (I could get it from an NSTextView, but I'd
rather not have to add a text view to just get a word count).
[demime 0.98b removed an attachment of type
application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.