【问题标题】:swift replace first occurrence of a String快速替换第一次出现的字符串
【发布时间】:2021-01-14 13:23:21
【问题描述】:

我有字符串"Das Auto ist blau, das andere ist allerdings blau"

我正在尝试仅将第一个 "blau" 替换为 "red"

我已经用.replacingOccurrences 尝试过,但这对我不起作用。

【问题讨论】:

    标签: ios swift string replace


    【解决方案1】:

    你可以试试

     let str = "Das Auto ist blau, das andere ist allerdings blau"
     if let range = str.range(of:"blau") {
         print(str.replacingCharacters(in: range, with:"red"))
     }
    

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 2016-11-02
      • 2011-06-05
      • 2016-08-07
      • 1970-01-01
      • 2022-10-07
      • 2014-07-08
      相关资源
      最近更新 更多