• 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: Manfred Bergmann <email@hidden>
  • Date: Wed, 26 Oct 2005 15:12:47 +1000

Actually, there are some alternatives to the NSScanner method.
You could so something like this:

NSArray *components = [myString componentsSeparatedByString:@" "];
int count = ([myString length] - ([components count] -1));

But I do not know what happend if there are more than one space character in a row. This has to be tested.

Or this:
NSMutableString *mutable = [NSMutableString stringWithString:myString];
int spaces = [mutable replaceOccurencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0,[mutable length])];



Regards, Manfred




Am 26.10.2005 um 14:10 schrieb Julian Sanchez:

Hello,

I recently started reading Hillegass' book on cocoa programming. I just finished Aaron's challenge at the end of chapter 4 in which you need to write a small application that allows a user to enter text, and you tell the user how many letters are contained in the text.

Easy enough since the "length" method in NSString does the job for you.

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?

Thanks for your help,
Julian

_______________________________________________
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


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

  • Prev by Date: runModalForWindow
  • Next by Date: Re: NSMailDelivery
  • 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