4

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?

Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75

3 Answers3

5

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];
mttrb
  • 8,297
  • 3
  • 35
  • 57
4

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!

Deviator
  • 688
  • 3
  • 7
  • Thanks a lot...How did i miss that?? Anyhow thanks for the solution. I was totally blank. – Pradeep Reddy Kypa Apr 13 '12 at 10:46
  • 3
    Why are you creating an `NSString` using `stringWithFormat:` which is then passed to `NSMutableString`'s `initWithString:` when you could just use `NSMutableString`'s `initWithFormat:`? – mttrb Apr 13 '12 at 10:57
  • Yes u were right. We can achieve the same by this way as well... 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=%@",yourFirstString,yourSecondString]; – Pradeep Reddy Kypa Apr 13 '12 at 13:01
2

My way would be:

NSMutableString *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=%@",yourFirstString,yourSecondString] mutableCopy] autorelease];

According to a hint from dreamlax you can also use:

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];
Community
  • 1
  • 1
Jonas Schnelli
  • 9,965
  • 3
  • 48
  • 60
  • 3
    Alternatively, skip the `mutableCopy`/`autorelease` and just use `[NSMutableString stringWithFormat:]` – dreamlax Apr 13 '12 at 10:40