How to trim leading and trailing white spaces of a string?

后端 未结 7 1482
别那么骄傲
别那么骄傲 2021-01-30 02:56

Which is the effective way to trim the leading and trailing white spaces of string variable in Go?

7条回答
  •  既然无缘
    2021-01-30 03:34

    For example,

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        s := "\t Hello, World\n "
        fmt.Printf("%d %q\n", len(s), s)
        t := strings.TrimSpace(s)
        fmt.Printf("%d %q\n", len(t), t)
    }
    

    Output:

    16 "\t Hello, World\n "
    12 "Hello, World"
    

提交回复
热议问题