• 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: NSScanner instert space every 2 characters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSScanner instert space every 2 characters


  • Subject: Re: NSScanner instert space every 2 characters
  • From: Ben Kazez <email@hidden>
  • Date: Wed, 27 Jul 2005 12:17:08 -0400

My apologies for not changing the subject line on my previous post. Here's the message again, for the sake of sites that organize Cocoa-dev messages based on subject lines. Sorry.
================
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) {
        // It would probably be faster to use appendString.
        [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: Setting vertical alignment for a NSTextFieldCell ?
  • Next by Date: Re: CoreData: How to create two separate in-memory stores?
  • Previous by thread: NSScanner instert space every 2 characters
  • Next by thread: NSButton Tool-tip
  • Index(es):
    • Date
    • Thread