Let's say you have a line:
var string = "potatoes + carrots"
And you want to replace the word "potato" in this line with "tomatoes"
string = string.replacingOccurrences(of: "potatoes", with: "tomatoes", options: NSString.CompareOptions.literal, range: nil)
If you print your line, now it will be: "tomatoes + carrots"
If you want to completely remove the word "potato" from a bite, you can use:
string = string.replacingOccurrences(of: "potatoes", with: "", options: NSString.CompareOptions.literal, range: nil)
If you want to use some other characters in your bite, use:
- Zero character (\ 0)
- Backslash (\)
- Horizontal tab (\ t)
- Line feed (\ n)
- Carriage Return (\ r)
- Double quote (\ ")
- Single quote (\ ')
Example:
string = string.replacingOccurrences(of: "potatoes", with: "dog\ toys", options: NSString.CompareOptions.literal, range: nil)
Conclusion: "dog toys + carrots"
Mini Official Aug 30 '18 at 8:10 2018-08-30 08:10
source share