How do I remove all Unicode newlines from a UTF-8 string in GoLang? I found this answer for PHP.
You can also use the strings. TrimSpace() function which will also remove any extra trailing or leading characters like the ones of regex \n , \r and this approach is much more cleaner.
Some people think Go strings are always UTF-8, but they are not: only string literals are UTF-8. As we showed in the previous section, string values can contain arbitrary bytes; as we showed in this one, string literals always contain UTF-8 text as long as they have no byte-level escapes.
LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.
Depending on the operating system (and/or file format) a line break is either \n , or it's \r\n , or it's \r .
You can use strings.Map
:
func filterNewLines(s string) string {
return strings.Map(func(r rune) rune {
switch r {
case 0x000A, 0x000B, 0x000C, 0x000D, 0x0085, 0x2028, 0x2029:
return -1
default:
return r
}
}, s)
}
playground
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