Replace <br> with \n in swift

Viewed 840

I get a string from server and it contains <br> and I want to replace it with \n

First I use this method:

 let description  = user.description.replacingFirstOccurrence(of: "<br>", with: "\n")

But because there is no space before <br> this method isn't working then I use the same method as this link

Replace <br> in an NSString with a new line

 let description = user?.description ?? ""
 let filteredDescription  = Array(description).reduce("") {$0 + ($1 == "<br>" ? "\n" : $1)}

But I get this error now:

Cannot invoke 'reduce' with an argument list of type '(String, @escaping (String, String) -> String)'

May I ask anyone to help me? Thanks

1 Answers

Try using replacingOccurrences instead. Please find the code below

let description = "This is a test for testing line break <br> Hope this works as expected."
let filteredDescription  = description.replacingOccurrences(of: "<br>", with: "\n") 
Related