如何将NSString插入NSMutableString?

4

我有一个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="];

我需要在"question_num="后插入一个字符串(One),在"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。 对此有什么建议吗?
3个回答

5
以下内容将创建一个无需保留的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];

如果您需要保留字符串,只需使用[[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];

你真的需要将它变成可变字符串吗?一旦创建,你会去改变它吗?如果不是的话,只需将 NSMutableString 改为 NSString,例如(这将返回一个自动释放的 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];

4

试试这个:

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];

告诉我这是否有帮助!


非常感谢...我怎么会错过那个??无论如何,感谢您提供的解决方案。我完全一片空白。 - Pradeep Reddy Kypa
3
为什么你在创建一个 NSString 时使用了 stringWithFormat:,然后将其传递给 NSMutableStringinitWithString:,而不是直接使用 NSMutableStringinitWithFormat: 呢? - mttrb
是的,你是对的。我们也可以通过这种方式实现相同的效果... 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

2
我的方法是:
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];

根据dreamlax的提示,您还可以使用以下方法:

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];

3
可以跳过mutableCopy/autorelease,直接使用[NSMutableString stringWithFormat:] - dreamlax

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接