Re: Newbie Question: Find spaces inside a string
Re: Newbie Question: Find spaces inside a string
- Subject: Re: Newbie Question: Find spaces inside a string
- From: Douglas Davidson <email@hidden>
- Date: Wed, 26 Oct 2005 11:10:47 -0700
On Oct 25, 2005, at 9:10 PM, Julian Sanchez wrote:
However, I tried to be a bit smart here and provide additional
functionality in which if a user enters a sentence (with its
corresponding spaces) I would like to find that out and count only
the letters.
First thing that came to mind was to write a loop that would use
NSString:characterAtIndex, traversing the string one character at a
time.
My question is: Surely there's a better (and quicker) way to do
this, isn't it? What way is that?
There are quite a few possibilities other than simply spaces and
letters. If you can figure out what sets of characters you are
actually interested in, you should be able to construct something
fairly simply using e.g. -[NSString
rangeOfCharacterFromSet:options:range:], or NSScanner's character set
methods.
If you are interested in counting words, you could use e.g. -
[NSAttributedString nextWordFromIndex:forward:], which is what is
used for option-arrowing. There is also -[NSSpellChecker
countWordsInString:language:], which may be more linguistically
sensitive, but you must have a fallback ready in case the current
spellchecker does not support word count and so returns -1.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden