• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Finding count of a character in a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Finding count of a character in a string (From: Paul Harvey <email@hidden>)
 >Re: Finding count of a character in a string (From: Jim Hamilton <email@hidden>)
 >Re: Finding count of a character in a string (From: Steve Palmer <email@hidden>)

  • Prev by Date: Re: Float behaviour
  • Next by Date: no mouseUp if mouseDown not overridden?
  • Previous by thread: Re: Finding count of a character in a string
  • Next by thread: NSArrayController selection issue
  • Index(es):
    • Date
    • Thread