'How can I find repeated words and separate from sentences?
For example. I have string value like this :
var str = "asdImverygreatfullasdandeverythingwillbegoodandIwillbehappy"
there is 'asd' and 'and' these two words repeat between sentences. I want to find these two words and remove from str. Is that possible?
Desire output will be :
str = "ImverygreatfulleverythingwillbegoodIwillbehappy"
Solution 1:[1]
You can do something like this : -
var str = "asdImverygreatfullasdandeverythingwillbegoodandIwillbehappy"
str = str.replacingOccurrences(of: "asd", with: "")
str = str.replacingOccurrences(of: "and", with: "")
print("Answer : - \(str)")
Your Answer will be
Answer : - ImverygreatfulleverythingwillbegoodIwillbehappy
Solution 2:[2]
An efficient way is to replace the substrings with help of Regular Expression
let str = "asdImverygreatfullasdandeverythingwillbegoodandIwillbehappy"
let cleaned = str.replacingOccurrences(of: "a(s|n)d", with: "", options: .regularExpression)
"a(s|n)d" means find both "asd" and "and"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Namra Parmar |
| Solution 2 | vadian |
