• 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: Cocoa-dev Digest, Vol 2, Issue 1155
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa-dev Digest, Vol 2, Issue 1155


  • Subject: Re: Cocoa-dev Digest, Vol 2, Issue 1155
  • From: Ben Kazez <email@hidden>
  • Date: Wed, 27 Jul 2005 09:11:04 -0400

On Wed, 27 Jul, Julien Palmas wrote:
I have a NSString and I would like to insert a space between every 2
characters. I just would like to know if NSScanner would be of any
use for this task or if I should iterate myself through the string.
Thanx in advance.

NSScanner is for isolating tokens from a string; you don't need it for iterating a string. Something like the following should work as a category on NSString:


// I'm on a PC at work. I haven't even tried compiling this, so treat
// it as a very rough estimate.
- (NSString *)stringByPaddingWithString:(NSString *)aString
                             atInterval:(unsigned int)interval {
    NSMutableString *ret = [self mutableCopy];

    // This puts aString at the beginning, too. I'm not sure if this is
    // what you want. You can start index out at paddingStringLength
    // instead of 0 to avoid this.
    unsigned int myLength = [self length];
    unsigned int paddingStringLength = [aString length];
    for (unsigned int index = 0; index < length; index += interval
        + paddingStringLength) {
        [ret insertString:aString atIndex:index];
    }
    return [ret autorelease];
}

Ben
--
Ben Kazez
http://www.benkazez.com/
_______________________________________________
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


  • Prev by Date: Re: NSThread, drawing and waiting
  • Next by Date: Re: How do you use AuthorizationRuleIsAdmin?
  • Previous by thread: Re: Text System Bugs
  • Next by thread: NSSet designated initializer
  • Index(es):
    • Date
    • Thread