我要做的是在字符串中使用占位符并将占位符替换为特定于用户的数据。我可以将占位符设置为任何内容,因此基本上我希望执行以下操作:
这就是我所拥有的。我目前有一个 url,其中包含一组这样的占位符。 http://example.com/resource?placeholder1=placeholder2或 http://placeholder1:placeholder2@example.com/something?placeholder3
如何正确标记占位符并替换它们?
预先感谢您的任何帮助。
最佳答案
您可以使用 stringByReplacingOccurrencesOfString 方法,如下所示
NSString *strUrl = @"http://example.com/resource?placeholder1=placeholder2";
strUrl = [strUrl stringByReplacingOccurrencesOfString:@"placeholder1" withString:@"value1"];
strUrl = [strUrl stringByReplacingOccurrencesOfString:@"placeholder2" withString:@"key1"];
关于ios - 替换字符串中的多个占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29161239/