Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang - Removing all Unicode newline characters from a string

Tags:

unicode

utf-8

go

How do I remove all Unicode newlines from a UTF-8 string in GoLang? I found this answer for PHP.

like image 415
simplfuzz Avatar asked Jul 11 '16 11:07

simplfuzz


People also ask

How do I remove N from string in Golang?

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.

Is golang string UTF8?

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.

What is Unicode for New Line?

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'.

How do I add a new line in Golang?

Depending on the operating system (and/or file format) a line break is either \n , or it's \r\n , or it's \r .


1 Answers

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

like image 84
OneOfOne Avatar answered Oct 13 '22 12:10

OneOfOne