I want to replace some text in a string with another string.
For example : A string equals to "Red small car". I want to replace "small" with "big" so the string becomes "Red big car". I have to do this in swift. Thanks.
You can try using stringByReplacingOccurrencesOfString
let string = "Big red car"
let replaced = (string as NSString).stringByReplacingOccurrencesOfString("Big", withString: "Small")
Edit In Swift 5
import Foundation
let string = "Big red car"
let replaced = string.replacingOccurrences(of: "Big", with: "Small")
You can use stringByReplacingOccurrencesOfString
, e.g.
let s1 : String = "Red small car"
let s2 = s1.stringByReplacingOccurrencesOfString("small", withString: "big")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With