I have the following string from a server:
I agree with the <a>((http://example.com)) Terms of Use</a> and I've read the <a>((http://example2.com)) Privacy</a>
now I want to show it like this in a label:
I agree with the <a href="http://google.com">Terms of Use</a> and I've read the <a href="http://google.com">Privacy</a>
I tried to cut of the ((http://example.com)) from the string and save it in another String. I need the link because the text should be clickable later.
I tried this to get the text that I want:
//the link:
let firstString = "(("
let secondString = "))"
let link = (text.range(of: firstString)?.upperBound).flatMap { substringFrom in
(text.range(of: secondString, range: substringFrom..<text.endIndex)?.lowerBound).map { substringTo in
String(text[substringFrom..<substringTo])
}
}
//the new text
if let link = link {
newString = text.replacingOccurrences(of: link, with: kEmptyString)
}
I got this from here: Swift Get string between 2 strings in a string
The problem with this is that it only removes the text inside the (( )) brackets. The brackets are still there. I tried to play with the offset of the indexes but this doesn't changed anything. Moreover this solution works if there's only one link in the text. If there are multiple links I think they should be stored and I have to loop through the text. But I don't know how to do this. I tried many things but I don't get this working. Is there maybe an easier way to get what I want to do?