Replacing Substrings

suggest change

Patterns 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 correct$ = regEx.stringByReplacingMatches(in: money, options: .withTransparentBounds, range: range, withTemplate: "$")
} catch let error as NSError {
    print("Matching failed")
}
//correct$ = "$¥€£$¥€£$"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents