I want to write a function that could be used like this:
let ๐ฉโ๐ฉโ๐งโ๐ฆ = "๐ฉโ๐ฉโ๐งโ๐ง".replacingFirstOccurrence(of: "๐ง", with: "๐ฆ")
Given how odd both this string and Swift's String
library are, is this possible in Swift?
Using the range(of:options:range:locale:)
the solution became quite concise:
extension String {
func replaceFirstOccurrence(of searchString: String, with replacementString: String) -> String {
guard let range = self.range(of: searchString, options: .literal) else { return self }
return self.replacingCharacters(in: range, with: replacementString)
}
}
This works by first finding the range of searchString
within the instance, and if a range is found the range is replaced with replacementString
. Otherwise the instance just returns itself. And, since the range(of:)
method returns as soon as it finds a match, the returned range is guaranteed to be the first occurrence.
"221".replaceFirstOccurrence(of: "2", with: "3") // 321
"๐ฉโ๐ฉโ๐งโ๐ฆ".replaceFirstOccurrence(of: "\u{1f469}", with: "\u{1f468}") // ๐จโ๐ฉโ๐งโ๐ฆ
*To clarify, the last test case converts woman-woman-girl-boy to man-woman-girl-boy.