• 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: 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.


  • Follow-Ups:
    • Re: Is Concatenation that complex?
      • From: "M. Uli Kusterer" <email@hidden>
    • Re: Is Concatenation that complex?
      • From: Finlay Dobbie <email@hidden>
    • [OT] Algebra (was: Re: Is Concatenation that complex?)
      • From: Kay Roepke <email@hidden>
References: 
 >Is Concatenation that complex? (From: Adam <email@hidden>)

  • Prev by Date: Re: Pop email checking information?
  • Next by Date: C function to a Selector ?
  • Previous by thread: Re: Is Concatenation that complex?
  • Next by thread: [OT] Algebra (was: Re: Is Concatenation that complex?)
  • Index(es):
    • Date
    • Thread