• 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: Jim Hamilton <email@hidden>
  • Date: Mon, 22 Aug 2005 09:31:51 -0400


On Aug 22, 2005, at 4:49 AM, Paul Harvey wrote:

To find out how many of a particular character there was in large string, I used 'componentsSeparatedByString' method of NSArray then used [array count] to find out how many there were.

Is there a better way, or is this a typical route?

NSArray *verses = [NSArray alloc];
verses = [chapterText componentsSeparatedByString:@"\n"];
verseCount = [verses count];

Hmm. This would work (aside from an off-by-1 error), but it seems wasteful; you create an array you don't use, which takes time and (at least temporarily) space.


You could accomplish the same thing (without that overhead) by iteratively calling rangeOfString:options:range:. [Or if, as above, you are looking specifically for line endings, use getLineStart:end:contentsEnd:forRange:, as it handles any line ending characters.]

Jim H
--
Jim Hamilton

email@hidden
email@hidden

_______________________________________________
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


  • Follow-Ups:
    • Re: Finding count of a character in a string
      • From: Steve Palmer <email@hidden>
References: 
 >Finding count of a character in a string (From: Paul Harvey <email@hidden>)

  • Prev by Date: WaekHashMap
  • Next by Date: Re: What To Do w/ ADC Ref Library Update
  • Previous by thread: Re: Finding count of a character in a string
  • Next by thread: Re: Finding count of a character in a string
  • Index(es):
    • Date
    • Thread