iphone - 如何将 NSString 插入 NSMutableString?

标签 iphone objective-c ios5 nsstring nsmutablestring

我有一个 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

对此有什么建议吗?

最佳答案

以下将创建一个未保留的 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];

关于iphone - 如何将 NSString 插入 NSMutableString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10139174/

相关文章:

ios - 在 iOS 中评价应用程序

iphone - 为什么 __NSCFDictionary 不是一种 NSDictionary 类

iphone - 替换出现的 NSString - iPhone

ios - 用于缩放和平移/倾斜的捏合手势

iphone - 不是由应用程序代码引起的泄漏 - 它会获得批准吗?

ios - 具有不同自动旋转选项的屏幕

objective-c - 在 Cocoa 中你更喜欢 NSInteger 还是 int,为什么?

objective-c - 与父类(super class)属性名称和协议(protocol)方法名称冲突

iphone - 将字符串作为 JSON 对象发送

iphone - 带有标题和背景图像的后退导航项