快速替换第一次出现的字符串

时间:2021-01-14 13:23:21

标签: ios swift string replace

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

我试图只用 "blau" 替换第一个 "red"

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

1 个答案:

答案 0 :(得分:0)

你可以试试

 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"))
 }
相关问题