kishor0011 kishor0011 - 3 years ago 160
Swift Question

How to replace a substring with a link(http) in swift 3?

I have a string and substring(http) and I want to replace that substring but I don't know when that substring will end. I mean want to check it until one space is not coming and after that I want to replace it.
I am checking that if my string contains http which is also a string then I want to replace it when space will come.

Here below is my example :-

let string = "Hello.World everything is good http://www.google.com By the way its good".


This is my string It can be dynamic also I mean in this above string http is there, so I want to replace "http://www.google.com" to "website".
So it would be

string = "Hello.World everything is good website By the way its good"

Answer Source

A possible solution is Regular Expression

The pattern searches for http:// or https:// followed one or more non-whitespace characters up to a word boundary.

let string = "Hello.World everything is good http://www.google.com By the way its good"
let trimmedString = string.replacingOccurrences(of: "https?://\\S+\\b", with: "website", options: .regularExpression)
print(trimmedString)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download