在Go中,哪一种有效的方法来修剪字符串变量的前导和尾随空白?


当前回答

我对性能很感兴趣,所以我做了一个比较,只修剪左边 的一面:

package main

import (
   "strings"
   "testing"
)

var s = strings.Repeat("A", 63) + "B"

func BenchmarkTrimLeftFunc(b *testing.B) {
   for n := 0; n < b.N; n++ {
      _ = strings.TrimLeftFunc(s, func(r rune) bool {
         return r == 'A'
      })
   }
}

func BenchmarkIndexFunc(b *testing.B) {
   for n := 0; n < b.N; n++ {
      i := strings.IndexFunc(s, func(r rune) bool {
         return r != 'A'
      })
      _ = s[i]
   }
}

func BenchmarkTrimLeft(b *testing.B) {
   for n := 0; n < b.N; n++ {
      _ = strings.TrimLeft(s, "A")
   }
}

TrimLeftFunc和IndexFunc是一样的,TrimLeft要慢一些:

BenchmarkTrimLeftFunc-12        10325200               116.0 ns/op
BenchmarkIndexFunc-12           10344336               116.6 ns/op
BenchmarkTrimLeft-12             6485059               183.6 ns/op

其他回答

我对性能很感兴趣,所以我做了一个比较,只修剪左边 的一面:

package main

import (
   "strings"
   "testing"
)

var s = strings.Repeat("A", 63) + "B"

func BenchmarkTrimLeftFunc(b *testing.B) {
   for n := 0; n < b.N; n++ {
      _ = strings.TrimLeftFunc(s, func(r rune) bool {
         return r == 'A'
      })
   }
}

func BenchmarkIndexFunc(b *testing.B) {
   for n := 0; n < b.N; n++ {
      i := strings.IndexFunc(s, func(r rune) bool {
         return r != 'A'
      })
      _ = s[i]
   }
}

func BenchmarkTrimLeft(b *testing.B) {
   for n := 0; n < b.N; n++ {
      _ = strings.TrimLeft(s, "A")
   }
}

TrimLeftFunc和IndexFunc是一样的,TrimLeft要慢一些:

BenchmarkTrimLeftFunc-12        10325200               116.0 ns/op
BenchmarkIndexFunc-12           10344336               116.6 ns/op
BenchmarkTrimLeft-12             6485059               183.6 ns/op
package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
}

输出: 你好,打地鼠

点击这个链接——https://golang.org/pkg/strings/#TrimSpace

@peterSO有正确答案。我在这里添加了更多的例子:

package main

import (
    "fmt"
    strings "strings"
)

func main() { 
    test := "\t pdftk 2.0.2  \n"
    result := strings.TrimSpace(test)
    fmt.Printf("Length of %q is %d\n", test, len(test))
    fmt.Printf("Length of %q is %d\n\n", result, len(result))

    test = "\n\r pdftk 2.0.2 \n\r"
    result = strings.TrimSpace(test)
    fmt.Printf("Length of %q is %d\n", test, len(test))
    fmt.Printf("Length of %q is %d\n\n", result, len(result))

    test = "\n\r\n\r pdftk 2.0.2 \n\r\n\r"
    result = strings.TrimSpace(test)
    fmt.Printf("Length of %q is %d\n", test, len(test))
    fmt.Printf("Length of %q is %d\n\n", result, len(result))

    test = "\r pdftk 2.0.2 \r"
    result = strings.TrimSpace(test)
    fmt.Printf("Length of %q is %d\n", test, len(test))
    fmt.Printf("Length of %q is %d\n\n", result, len(result))   
}

你也可以在围棋场上找到它。

就像@Kabeer提到的,你可以使用TrimSpace,这里有一个来自golang文档的例子:

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
}

为了修剪你的字符串,Go的“strings”包有TrimSpace(), Trim()函数来修剪前导和尾随空格。

有关更多信息,请查看文档。