• 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: counting characters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: counting characters


  • Subject: Re: counting characters
  • From: Allan Odgaard <email@hidden>
  • Date: Thu, 15 Apr 2004 19:50:49 +0200

On 15. Apr 2004, at 17:52, Jonathan E. Jackel wrote:

The most compact code for counting characters that I can come up with is:

Compact? Then how about:
NSString* str = @"Hello world";

// count number of o's in string
occurrences = std::count(beginof(str), endof(str), 'o');

// count number of letters in string
occurrences = std::count_if(beginof(str), endof(str), is_letter);

Where is_letter could be:
bool is_letter (unichar ch)
{
static NSCharacterSet* letters = [NSCharacterSet letterCharacterSet];
return [letters characterIsMember:ch];
}

Or it could be made using std::bind and unary_method :) of cause the above is using CocoaSTL.






** Cocoa FAQ: <http://www.alastairs-place.net/cocoa/faq.txt> **
_______________________________________________
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.


  • Follow-Ups:
    • RE: counting characters
      • From: "Jonathan E. Jackel" <email@hidden>
References: 
 >RE: counting characters (From: "Jonathan E. Jackel" <email@hidden>)

  • Prev by Date: Re: New book on its way!!!
  • Next by Date: RE: counting characters
  • Previous by thread: RE: counting characters
  • Next by thread: RE: counting characters
  • Index(es):
    • Date
    • Thread