可以使用字符串的replaceFirst()方法来替换第一个出现的内容。该方法接受两个参数:要替换的字符串和替换后的字符串。 例如,要将字符串中的第一个“hello”替换为“hi”,可以使用以下代码: ``` var str = "hello world hello" if let range = str.range(of: "hello") { str.replaceSubrange(range, with: "hi") } print(str) // 输出:hi world hello ``` 在上面的代码中,首先定义了一个字符串变量str,它包含了两个“hello”。然后使用range(of:)方法找到第一个“hello”的范围,并将其存储在range变量中。最后,使用replaceSubrange()方法将该范围内的内容替换为“hi”。