Re: Is Concatenation that complex?
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.