替换字符串不起作用

时间:2012-11-12 12:39:18

标签: objective-c nsstring nsmutablestring

我需要在“id =”

之后添加字符串@“d”

示例=

url = @"id=63488320543140151742289377"

需要

url = @"id=d63488320543140151742289377"

我试过这个:

NSRange range = [url rangeOfString:@"id="];
[url stringByReplacingCharactersInRange:range withString:@"id=d"];

但没有工作

2 个答案:

答案 0 :(得分:7)

url = [url stringByReplacingOccurrencesOfString:@"id=" withString:@"id=d"];

答案 1 :(得分:2)

stringByReplacingCharactersInRange返回一个字符串。如果您将返回的新字符串分配给某个变量(例如url = [url stringByReplacingCharactersInRange:range withString:@"id=d"];

,则您的代码应该有效