7

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.

1 Answer 1

14

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

NSString *apiKey = @"bfc9c6ddeea9d75345cd";
NSString* curl = [NSString stringWithFormat:@"https://invoices?ticket=%@&apikey=%@", self.ticket, apiKey];
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.