Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

🚧 Remove non-printable characters from a string in Go

strings

Please consider supporting us by disabling your ad blocker

When working with external files or user input, it is often a good idea to remove invisible characters that can cause problems. These characters are “non-printable” - they do not occupy a space in printing and fall under the Other or Separator category in the Unicode standard. For example, non-printable are:

To remove non-printable characters from a string in Go, you should iterate over the string and check if a given rune is printable using the unicode.IsPrint() function. If not, the rune should be ignored, otherwise it should be added to the new string.

Instead of iterating and manually creating a new string in the for loop, you can use the strings.Map(), which returns a copy of the string with all characters modified according to the mapping function. The best part is that the character is dropped if the mapping function returns a negative value for a given rune. So, we can return -1 for a non-printable character, and an unmodified rune if the unicode.IsPrint() returns true. See the following example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
    "fmt"
    "strings"
    "unicode"
)

func main() {
    text := "b\u00a0e\u200bhind\n"

    fmt.Println(text)
    fmt.Println(len(text))
    fmt.Println("---")

    text = strings.Map(func(r rune) rune {
        if unicode.IsPrint(r) {
            return r
        }
        return -1
    }, text)

    fmt.Println(text)
    fmt.Println(len(text))
}

Output

b e​hind

12
---
behind
6

The unicode.IsPrint() returns true for:

There is also a function unicode.IsGraphic(), that works almost the same, except that it returns true for all space characters in the category Zs of the Unicode standard.

🧽 Remove non-alphanumeric characters from a string in Go

Learn how to remove characters that are not letters or numbers from a string
strings regex

🎲 Generate a random string in Go

Learn how to generate a random string of a fixed length
introduction random strings

✌️ 4 ways to create or print string with double quotes in Go

Learn how to create and print double-quoted string
introduction strings