concatenate string stringWithFormat

Discussion in 'iOS Programming' started by vidyareef, Jan 20, 2009.


Was this something new to you?

Poll closed Jan 30, 2009.
  1. Yes

  2. No

  1. vidyareef macrumors newbie

    Jan 11, 2009

    to concatenate the string with a wriiten text
    (here Hello world )
    and a value existing in the code
    we usually use this
    myLabel.text= [NSString stringWithFormat:mad:"Hello world ", myString];

    But now if i have two values existing in the code e.g. inforesult and myString how can i concatenate them together ?

    i tried
    myLabel.text= [NSString stringWithFormat: inforesult, myString];

    but it doesn't work. Returns me only the inforesult value displayed.

    How can i solve this?
  2. jnic macrumors 6502a

    Oct 24, 2008
    [NSString stringWithFormat:@"%@%@", inforesult, myString];
    As always, it's in the documentation.
  3. vidyareef thread starter macrumors newbie

    Jan 11, 2009
    while searching i also got
    NSString *string1= Inforesultat;
    NSString *string2 = myString;
    string1 = [string1 stringByAppendingString:string2];

    I wonder what is the difference and which one is better to use?
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    stringByAppendingString is probably a little more efficient. stringWithFormat is a general method that can do a lot of things. Obviously it has to parse the format string and then apply the parameters to the parsed format. stringByAppendingString does one thing only so can be simpler and faster.

    Having said that it probably will make little difference in most cases and my analysis is based on my assumptions and not on knowledge of the implementation.
  5. jeremy.king macrumors 603


    Jul 23, 2002
    Fuquay Varina, NC
    I'd like to believe using NSMutableString would be the most efficient...or at least, the most appropriate

    IMO, if you are worried about String append operations, I think you're over optimizing...

Share This Page