• 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: Is Concatenation that complex?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Concatenation that complex?


  • Subject: Re: Is Concatenation that complex?
  • From: Gwynne <email@hidden>
  • Date: Tue, 27 Apr 2004 16:39:34 -0400

On Apr 27, 2004, at 3:44 PM, Bob Ippolito wrote:
For joining a lot of strings together without operator support it probably makes sense to use a format or to use an array join rather than doing lots of 2-string concatenation.

It is my experience that a great many people, especially those without any UNIX experience or who learned Mac programming without doing console-style code first, miss the power and potential of printf()-style format specifiers, especially as supported by NSString's ...WithFormat[AndArguments] methods.

prependedString = [NSString stringWithFormat:@"%@ %@", firstString, prependedString];

Yes, that's perfectly legal to do, even though it'd be foolish with sprintf(). Keep an eye on your memory management, though; if the original string was allocated by you and not autoreleased, you'll need to either autorelease it or store the result in a separate varaible.

I recommend in general that anyone not familiar with printf() take the time to do "man 3 printf" in Terminal and read the manpage through thoroughly to learn how to use format specifiers. The only difference between printf() specifiers and NSString/CFString specifiers is that the latter add %C (Unicode/wchar_t version of %c), %S (Unicode/wchar_t version of %s), and %@ (any Cocoa or CoreFoundation object, as the result of [object description] or CFCopyDescription(object).) Someone correct me if I forgot or misrepresented any :).

Some useful uses of format specifiers:

stringRepresentationOfOSType = [NSString stringWithFormat:@"%.4s", &osTypeVar];
stringRepresentationOfPointer = [NSString stringWithFormat:@"0xlX", ptr];
gramaticalString = [NSString stringWithFormat:@"one%s", isPlural ? "s" : ""];
formedURL = [NSString stringWithFormat:@"http://%@/%@";, server, path]; // NSURL would be better for this
stringRepresentationOfTwoPlaceRoundedDecimal = [NSString stringWithFormat:@".2f", decimalNum];

-- Gwynne, key to the Code that runs us all
Formerly known as Sailor Quasar.
Email: email@hidden
Web: http://musicimage.plasticchicken.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • NSString format specifiers (was: Is Concatenation that complex?)
      • From: Allan Odgaard <email@hidden>
References: 
 >Is Concatenation that complex? (From: Adam <email@hidden>)
 >Re: Is Concatenation that complex? (From: Marco Scheurer <email@hidden>)
 >Re: Is Concatenation that complex? (From: Finlay Dobbie <email@hidden>)
 >Re: Is Concatenation that complex? (From: Bob Ippolito <email@hidden>)

  • Prev by Date: Re: Is Concatenation that complex?
  • Next by Date: Re: Getting hold of a Documents Window for displaying a Sheet [SOLVED]
  • Previous by thread: Re: Is Concatenation that complex?
  • Next by thread: NSString format specifiers (was: Is Concatenation that complex?)
  • Index(es):
    • Date
    • Thread