• 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: Newbie Question: Find spaces inside a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Question: Find spaces inside a string


  • Subject: Re: Newbie Question: Find spaces inside a string
  • From: Douglas Davidson <email@hidden>
  • Date: Wed, 26 Oct 2005 11:10:47 -0700


On Oct 25, 2005, at 9:10 PM, Julian Sanchez wrote:

However, 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?


There are quite a few possibilities other than simply spaces and letters. If you can figure out what sets of characters you are actually interested in, you should be able to construct something fairly simply using e.g. -[NSString rangeOfCharacterFromSet:options:range:], or NSScanner's character set methods.


If you are interested in counting words, you could use e.g. - [NSAttributedString nextWordFromIndex:forward:], which is what is used for option-arrowing. There is also -[NSSpellChecker countWordsInString:language:], which may be more linguistically sensitive, but you must have a fallback ready in case the current spellchecker does not support word count and so returns -1.

Douglas Davidson

_______________________________________________
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: Newbie Question: Find spaces inside a string
      • From: Ricky Sharp <email@hidden>
References: 
 >Newbie Question: Find spaces inside a string (From: Julian Sanchez <email@hidden>)

  • Prev by Date: Re: Generating ligature glyphs
  • Next by Date: Re: Using an NSString as standard input to an NSTask
  • Previous by thread: Re: Newbie Question: Find spaces inside a string
  • Next by thread: Re: Newbie Question: Find spaces inside a string
  • Index(es):
    • Date
    • Thread