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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误