go - 如何返回空白 rune

标签 go rune

我正在查看 string.Map 函数,它必须采用返回 rune 的映射函数。我想通过调用来消除解析为 false 的 rune :unicode.IsPrint()

func Map(映射 func(rune) rune, s string) string

我的函数看起来像这样:

func main() { 
func CleanUp(s string) string {

    clean := func(r rune) rune {
        if unicode.IsPrint(r) || r == rune('\n') {
            return r
        }
        return rune('')
    }

strings.Map(clean, s)
}

它应该将像这样的 "helloworld '\x10" 清理成 "helloworld ' "

但是 rune('') 是无效的。如何返回空白或空 rune ?

最佳答案

如果你想消除 rune ,“空白 rune ”不是办法。那不会消除任何东西。

假设您正在谈论 strings.Map ,文档说

If mapping returns a negative value, the character is dropped from the string with no replacement.

让您的映射器返回负值以指示应丢弃 rune 。

关于go - 如何返回空白 rune ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174097/

相关文章:

go - undefined variable golang

go - 使用go for openapi生成器时如何指定模块路径?

go - 如何区分 typeswitch 中的 rune 和 int32 值?

unicode - 如何从golang中的一串 rune 中获取子串?

go - 使用struct作为包装器

go - 如何使用 go 访问和修改备用数据流 (ADS)

mongodb - 如何使用官方的 mongo-go-driver 连接到 MongoDB Atlas

go - 什么是 rune ?

string - 在 go 中迭代 2 个字符串

Go rune literal for high positioned emojis