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

    50.0%
  2. No

    50.0%
  1. macrumors newbie

    Joined:
    Jan 11, 2009
    #1
    Hello!


    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. macrumors 6502a

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

    Joined:
    Jan 11, 2009
    #3
    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. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #5
    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