Eliminating Invalid UTF-8 Characters in a String in Go
Encoding/decoding data using UTF-8 ensures compatibility across different systems and character sets. However, invalid UTF-8 characters can arise due to transmission errors, malicious attacks, or other factors. Removing these characters is essential for data integrity and proper JSON encoding.
Solution:
To address this issue in Go, there are several approaches available:
Go 1.13 :
Example:
fixedString := strings.ToValidUTF8("a\xc5z", "")
Go 1.11 :
Example:
fixUtf := func(r rune) rune { if r == utf8.RuneError { return -1 } return r } fixedString := strings.Map(fixUtf, "a\xc5z")
The above is the detailed content of How Do I Remove Invalid UTF-8 Characters from a Go String?. For more information, please follow other related articles on the PHP Chinese website!