dskpywvki951583595 2019-01-21 07:40
浏览 149
已采纳

从字符串中删除空字符

I want to check if string is empty and parse the string in time.

Please find the below code

valueStr = strings.Replace(string(valueStr), " ", "", -1)
valueStr = strings.Replace(string(valueStr), "\t", "", -1)
valueStr = strings.Replace(string(valueStr), "
", "", -1)
valueStr = strings.Replace(string(valueStr), "", "", -1)
var re = regexp.MustCompile(`\s`)
valueStr = re.ReplaceAllString(valueStr, "")

if valueStr != "" {
    fmt.Printf("-------- valueStr %c: 
", valueStr)         // o/p =>  -------- valueStr %!c(string= ):
    fmt.Printf("-------- valueStr %#v: 
", valueStr)        // o/p => -------- valueStr "\x00":
    fmt.Printf("-------- valueStr %x: 
", valueStr)         // o/p =>  -------- valueStr 00:
    fmt.Println("-------- valueStr length: ", len(valueStr)) // o/p => -------- valueStr length:  1

    // considering valueStr is not empty, parse string to time

    time, err := time.Parse(TIME_FORMAT, strings.TrimSpace(valueStr))
    if err != nil {
        fmt.Println("-------- Error converting time: ", err) // o/p => -------- Error converting time:  parsing time " " as "15:04:05": cannot parse " " as "15"
        return
    }
} else {
    // another code
}

How to remove this empty character from string? Or check if string contains this empty character?

  • 写回答

1条回答 默认 最新

  • douhuxi4145 2019-01-21 08:25
    关注

    You can remove \x00 runes from a string the same way you can remove any other runes:

    valueStr = strings.Replace(valueStr, "\x00", "", -1)
    

    Example:

    s := "a\x00b"
    fmt.Printf("%q
    ", s)
    s = strings.Replace(s, "\x00", "", -1)
    fmt.Printf("%q
    ", s)
    

    Output (try it on the Go Playground):

    "a\x00b"
    "ab"
    

    Using strings.Replacer

    Also note that you can substitute the multiple replaces with a single operation by using strings.Replacer, and it will also be more efficient as it only iterates over the input once (and there will be only one string allocated for the result, no matter how many substrings you want to replace).

    For example:

    s := " \t
    abc\x00"
    fmt.Printf("%q
    ", s)
    
    r := strings.NewReplacer(" ", "", "\t", "", "
    ", "", "", "", "\x00", "")
    s = r.Replace(s)
    fmt.Printf("%q
    ", s)
    

    Output (try it on the Go Playground):

    " \t
    abc\x00"
    "abc"
    

    Also note that it's enough to create a string.Replacer once, and you can store it in a (global) variable and reuse it, it is even safe to use it concurrently from multiple goroutines.

    Using strings.Map()

    Also note that if you only want to replace (remove) single runes and not multi-rune (or multi-byte) substrings, you can also use strings.Map() which might be even more efficient than strings.Replacer.

    First define a function that tells which runes to replace (or remove if you return a negative value):

    func remove(r rune) rune {
        switch r {
        case ' ', '\t', '
    ', '', 0:
            return -1
        }
        return r
    }
    

    And then using it:

    s := " \t
    abc\x00"
    fmt.Printf("%q
    ", s)
    
    s = strings.Map(remove, s)
    fmt.Printf("%q
    ", s)
    

    Output (try it on the Go Playground):

    " \t
    abc\x00"
    "abc"
    

    Benchmarks

    We might think strings.Map() will be superior as it only have to deal with runes which are just int32 numbers, while strings.Replacer have to deal with string values which are headers (length+data pointer) plus a series of bytes.

    But we should know that string values are stored as UTF-8 byte sequences in memory, which means strings.Map() have to decode the runes from the UTF-8 byte sequence (and encode the runes back to UTF-8 in the end), while strings.Replacer does not: it may simply look for byte sequence matches without decoding the runes. And strings.Replacer is highly optimized to take advantage of such "tricks".

    So let's create a benchmark to compare them:

    We'll use these for the benchmarks:

    var r = strings.NewReplacer(" ", "", "\t", "", "
    ", "", "", "", "\x00", "")
    
    func remove(r rune) rune {
        switch r {
        case ' ', '\t', '
    ', '', 0:
            return -1
        }
        return r
    }
    

    And we run benchmarks on different input strings:

    func BenchmarkReplaces(b *testing.B) {
        cases := []struct {
            title string
            input string
        }{
            {
                title: "None",
                input: "abc",
            },
            {
                title: "Normal",
                input: " \t
    abc\x00",
            },
            {
                title: "Long",
                input: "adsfWR \tab
    c\x00 \t
    abc\x00asdfWER
    ",
            },
        }
    
        for _, c := range cases {
            b.Run("Replacer-"+c.title, func(b *testing.B) {
                for i := 0; i < b.N; i++ {
                    r.Replace(c.input)
                }
            })
            b.Run("Map-"+c.title, func(b *testing.B) {
                for i := 0; i < b.N; i++ {
                    strings.Map(remove, c.input)
                }
            })
        }
    
    }
    

    And now let's see the benchmark results:

    BenchmarkReplaces/Replacer-None-4    100000000   12.3 ns/op    0 B/op  0 allocs/op
    BenchmarkReplaces/Map-None-4         100000000   16.1 ns/op    0 B/op  0 allocs/op
    BenchmarkReplaces/Replacer-Normal-4  20000000    92.7 ns/op    6 B/op  2 allocs/op
    BenchmarkReplaces/Map-Normal-4       20000000    92.4 ns/op   16 B/op  2 allocs/op
    BenchmarkReplaces/Replacer-Long-4     5000000   234 ns/op     64 B/op  2 allocs/op
    BenchmarkReplaces/Map-Long-4          5000000   235 ns/op     80 B/op  2 allocs/op
    

    Despite expectations, string.Replacer performs pretty good, just as good as strings.Map() due to it not having to decode and encode runes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计