Re: Finding count of a character in a string
Re: Finding count of a character in a string
- Subject: Re: Finding count of a character in a string
- From: Clark Cox <email@hidden>
- Date: Mon, 22 Aug 2005 14:44:06 -0400
2005/8/22, Steve Palmer <email@hidden>:
> Iterating over the string and calling [characterAtIndex:] might be
> sufficient. After all, regardless of the length of the string, any
> function that is going to be scanning for an occurrence of a
> particular character is going to be working in O(n) time at best. Or
> if you know up-front that the string is entirely ASCII, then getting
> a pointer to the raw string data with [UTF8String:] and iterating
> over it with a pointer might be marginally faster.
Even if the string isn't all ASCII, but the character that you're
searching for is, UTF8String will work, because UTF-8 is constructed
in a way that there is no way to confuse a single ASCII character with
part of a multi-octet character:
const char *utf8 = [string UTF8String];
unsigned count = 0;
for(;*utf8 != '\0'; ++utf8)
{
if(*utf8 == '\n')
{
++count;
}
}
--
Clark S. Cox III
email@hidden
http://clarkcox3.livejournal.com/
http://homepage.mac.com/clarkcox3/
_______________________________________________
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