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=”之后插入一个字符串(一个),在“ansValue=”之后插入另一个字符串(一个),所以我的最终字符串应该是

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

对此有何建议?

4

3 回答 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];

你真的需要它是一个可变的字符串,一旦它被创建,你会改变它吗?如果不是简单地将 更改NSMutableStringNSString,例如(这将返回一个 autoreleased 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];
于 2012-04-13T10:38:50.420 回答
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];

告诉我它是否有帮助!

于 2012-04-13T10:32:40.647 回答
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];
于 2012-04-13T10:37:05.043 回答