如何将NSString插入NSMutableString?

Pra*_*ypa 4 iphone objective-c nsstring nsmutablestring ios5

我有一个NSMutableString,我需要在两个地方插入另一个NSString.我的NSMutableString是

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="];
Run Code Online (Sandbox Code Playgroud)

我需要在"question_num ="之后插入一个String(一个),在"ansValue ="之后插入另一个字符串(One),所以我的最终字符串应该像

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

有什么建议吗?

mtt*_*trb 5

以下将创建一个untained 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];
Run Code Online (Sandbox Code Playgroud)

如果您需要保留它,只需使用[[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];
Run Code Online (Sandbox Code Playgroud)

你真的需要它成为一个可变的字符串,你是否会在创建后更改它?如果不是简单地更改NSMutableStringNSString,例如(这将返回自动释放NSString,[NSString alloc] initWithFormat:]如果您需要保留则使用):

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];
Run Code Online (Sandbox Code Playgroud)