6

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

Community
  • 1
  • 1
simplfuzz
  • 12,479
  • 24
  • 84
  • 137

1 Answers1

5

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

OneOfOne
  • 95,033
  • 20
  • 184
  • 185