views:

206

answers:

1

Hi Guys,

I want to append the string into single varilable using stringWithFormat.I knew it in using stringByAppendingString. Please help me to append using stringWithFormat for the below code.

NSString* curl = @"https://invoices?ticket=";
curl = [curl stringByAppendingString:self.ticket];
curl = [curl stringByAppendingString:@"&apikey=bfc9c6ddeea9d75345cd"];
curl = [curl stringByReplacingOccurrencesOfString:@"\n" withString:@""];

Thank You, Madan Mohan.

+2  A: 

You can construct your curl string using -stringWithFormat: method:

NSString *apiKey = @"bfc9c6ddeea9d75345cd";
NSString* curl = [NSString stringWithFormat:@"https://invoices?ticket=%@&apikey=%@", self.ticket, apiKey];
Vladimir
To expand on this, to append objects to a string, %@ is used such that in the above example, [self.ticket description] and [apiKey description] will be substituted in. The pass to -description is implicit. This will also let you pass in things like NSNumber using the same syntax. If you want to pass in any other datatype than objc objects, the same modifier syntax is supported as with printf() and friends; see their respective manual pages for information on what each option does.
jer