Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a NSString into a NSMutableString?

I have a NSMutableString where i need to insert another NSString at two places. My NSMutableString is

NSMutableString *webServiceLinkWithResponses = [[NSMutableString alloc] initWithString:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=&ansValue="];

I need to insert one String(One) after "question_num=" and another string(One) after "ansValue=" so my final string should be like

http://lmsstaging.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=One&ansValue=One

Any suggestions on this?

like image 987
Pradeep Reddy Kypa Avatar asked Dec 21 '22 23:12

Pradeep Reddy Kypa


2 Answers

The following will create an unretained NSMutableString:

NSMutableString *webServiceLinkWithResponses = [NSMutableString stringWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo];

If you need it to be retained simply use [[NSMutableString alloc] initWithFormat:@"..."]:

NSMutableString *webServiceLinkWithResponses = [[NSMutableString alloc] initWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo];

Do you really need it to be a mutable string, are you going to change it once it has been created? If not simply change the NSMutableString to NSString, e.g. (this returns an autoreleased NSString, use [NSString alloc] initWithFormat:] if you need it retained):

NSString *webServiceLinkWithResponses = [NSString stringWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo];
like image 69
mttrb Avatar answered Jan 05 '23 11:01

mttrb


Try this:

NSMutableString *webServiceLinkWithResponses = [[NSMutableString alloc] initWithString:[NSString stringWithFormat: @"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@",yourFirstString,yourSecondString];

Tell me if it helps!

like image 21
Deviator Avatar answered Jan 05 '23 10:01

Deviator