Replacing Substrings
suggest changePatterns can be used to replace part of an input string.
The example below replaces the cent symbol with the dollar symbol.
var money = "¢¥€£$¥€£¢"
let pattern = "¢"
do {
let regEx = try NSRegularExpression (pattern: pattern, options: [])
let nsString = money as NSString
let range = NSMakeRange(0, nsString.length)
let corrected = regEx.stringByReplacingMatches(in: money, options: .withTransparentBounds, range: range, withTemplate: "$")
// corrected == "$¥€£$¥€£$"
} catch {
print("Matching failed")
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents