skip to Main Content

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"

2

Answers


  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
    
    Login or Signup to reply.
  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"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search