PDA

View Full Version : concatenate string stringWithFormat




vidyareef
Jan 20, 2009, 03:21 AM
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:@"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?



jnic
Jan 20, 2009, 04:11 AM
[NSString stringWithFormat:@"%@%@", inforesult, myString];

As always, it's in the documentation (http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/Strings/Articles/FormatStrings.html).

vidyareef
Jan 20, 2009, 04:20 AM
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?

PhoneyDeveloper
Jan 20, 2009, 09:31 AM
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.

jeremy.king
Jan 20, 2009, 10:32 AM
I'd like to believe using NSMutableString (http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html) 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...