This feels like one of those operations that must be common enough that this is a duplicate question, but since I was not able to find it anywhere:
What is the most straightforward way to replace HTML entities with their string representations in a Go string
? That is, how would I turn the string Rø&d grød & fløde
into Rød grød & fløde
?
My own solutions have been either using strings.Replace
on all relevant entities (which quickly becomes intractable), or wrapping the string into an XML document and decoding it with xml.Decoder
(which seems silly and leads to numerous edge cases).
Use the html.UnescapeString function:
fmt.Println(html.UnescapeString("Rø&d grød & fløde"))
playground example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With