ios - 替换字符串中的多个占位符

标签 ios

我要做的是在字符串中使用占位符并将占位符替换为特定于用户的数据。我可以将占位符设置为任何内容,因此基本上我希望执行以下操作:

  • 在字符串中设置占位符(最多 4 个占位符)
  • 用我指定的字符串替换这些占位符

  • 这就是我所拥有的。我目前有一个 url,其中包含一组这样的占位符。 http://example.com/resource?placeholder1=placeholder2http://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/

    相关文章:

    ios - 从 AirPod 麦克风录制音频

    iOS:Facebook SDK 位置为空

    ios - 添加 subview 后,Swift Playground UITable View Cell 未调整大小

    ios - UINavigationBar 框架动画不可能?

    ios - 为什么 CGImage 数据不能在往返中存活下来?

    ios - 分段控件在第一段中显示奇怪的标题

    ios - 自定义 TableView 永不更新

    android - Flutter:如何打开 main.dart

    ios - SwiftUI 中的导航内容

    ios - Swift 3 - Tesseract 识别任何图像处理的抛出错误