Re: Is Concatenation that complex?
Re: Is Concatenation that complex?
- Subject: Re: Is Concatenation that complex?
- From: Marco Scheurer <email@hidden>
- Date: Fri, 23 Apr 2004 10:40:23 +0200
On Apr 23, 2004, at 4:58 AM, Adam wrote:
Newbie coming from java here.
I am looking to concatenate strings. In java I could do complex string
concatenation mixing variables and string literals in one line:
String middle = "insane root";
String RelevantQuote = "Or have we eaten on the" + middle + "that
takes the reason prisoner?"
I cannot resist mentioning how ugly using "+" for concatenation is
(IMHO). In my book "+" is used for, among other properties, commutative
operations. Here a+b is not the same as b+a. Any other symbol would
have been better, for instance ",", or maybe even no symbol at all.
or in my specific less interesting case I want to end up with "(20)"
String cookie = "20";
String messageString2 = "(" + cookie + ")";
From what I have read in Obj-C I have to do this:
[...]
It seams like a lot of work to do something simple. Is there a better
way?
Others already pointed showed how to use stringWithFormat for that
particular case. To join multiple strings you could also do:
[[NSArray arrayWithObjects:
@"Or have we eaten on the",
middle,
@"that takes the reason prisoner",
nil]
componentsJoinedByString:@""];
I agree, this is still not particularily nice. Using a variadic macro,
something like:
#define JoinedStrings(...) [[NSArray arrayWithObjects:__VA_ARGS__,
nil] componentsJoinedByString:@""]
should allow you to write:
JoinedStrings (@"Or have we eaten on the", middle, @"that takes the
reason prisoner?");
Marco Scheurer
Sen:te, Lausanne, Switzerland
http://www.sente.ch
_______________________________________________
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.