如何返回一个空白符文

use*_*520 5 go rune

我正在查看string.Map必须采用返回符文的映射函数的函数。我想通过调用以下命令来消除解析 false 的符文:unicode.IsPrint()

func Map(mapping 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)
}
Run Code Online (Sandbox Code Playgroud)

它应该清理的东西像这样"helloworld ' \x10""helloworld ' "

rune('')无效。我怎样才能返回一个空白或空的符文?

Ull*_*kut 5

\0据我了解,符文实际上是映射到 unicode 字符的整数值,因此如果条件检查失败,这段代码实际上返回该字符:

package main

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

func main() {
    fmt.Println(CleanUp("helloworld ' \x10"))
}

func CleanUp(s string) string {

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

    return strings.Map(clean, s)
}
Run Code Online (Sandbox Code Playgroud)

输出

helloworld '


use*_*ica 5

如果要消除符文,“空白符文”不是办法。那不会消除任何东西。

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

如果映射返回负值,则从字符串中删除该字符而不进行替换。

让你的映射器返回一个负值来指示符文应该被丢弃。