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: Prachi Gauriar <email@hidden>
- Date: Wed, 26 Oct 2005 00:57:23 -0400
On Oct 26, 2005, at 12:10 AM, Julian Sanchez wrote:
... you tell the user how many letters are contained in the text...
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?
You could use NSScanner and just read alphanumerics. NSScanner is
just an OO version of scanf, so I'm not sure how fast it is. Written
in Mail, so the usual disclaimers apply.
NSScanner *scanner = [NSScanner scannerWithString:myString];
NSCharacterSet *alphanumerics = [NSCharacterSet
alphanumericCharacterSet];
NSString *scannedString = nil;
unsigned count = 0;
while (![scanner isAtEnd]) {
[scanner scanUpToCharactersFromSet:alphanumerics intoString:nil];
if ([scanner scanCharactersFromSet:alphanumerics
intoString:&scannedString]) {
count += [scannedString length];
}
}
Hope that helps.
-Prachi
_______________________________________________
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