我有这样的一片

v := []string{"this", "is", "a", "sample", "string"}
我想创建ThisIsASampleString。我唯一想到的方法是先遍历 slice ,将它们转换为标题大小写,然后再加入它们,即
x := []string{}
for _, s := range v {
    x = append(x, strings.Title(s))
}

strings.Join(x[0:], "")
有更好的办法吗?

最佳答案

基准测试(执行版本go1.15.6 linux/amd64):

BenchmarkBuilder-8     1480506  792 ns/op  112 B/op  8 allocs/op
BenchmarkBuffer-8      1500859  843 ns/op  144 B/op  7 allocs/op
BenchmarkJoin-8        1350139  938 ns/op  160 B/op  7 allocs/op
BenchmarkReplaceAll-8  1513950  793 ns/op  128 B/op  4 allocs/op
BenchmarkLoop-8        2322938  536 ns/op  112 B/op  2 allocs/op
  • 使用strings.Builder:
  • sb := new(strings.Builder)
    for _, s := range v {
        sb.WriteString(strings.Title(s))
    }
    st := sb.String()
    
  • 使用bytes.Buffer:
  • p := new(bytes.Buffer)
    for _, s := range v {
        p.WriteString(strings.Title(s))
    }
    st = p.String()
    
  • 使用strings.Join(x, ""):
  • x := make([]string, len(v))
    for i, s := range v {
        x[i] = strings.Title(s)
    }
    st = strings.Join(x, "")
    
  • 单线(注意:输入元素内没有空格)
  • st = strings.ReplaceAll(strings.Title(strings.Join(v, " ")), " ", "")
    
  • 基本循环:
  • n := 0
    for _, s := range v {
        n += len(s)
    }
    x := make([]rune, 0, n)
    for _, s := range v {
        for i, r := range s {
            if i == 0 {
                r = unicode.ToUpper(r)
            }
            x = append(x, r)
        }
    }
    st = string(x)
    

    代码:
    package main
    
    import (
        "bytes"
        "strings"
        "testing"
        "unicode"
    )
    
    func BenchmarkBuilder(b *testing.B) {
        for i := 0; i < b.N; i++ {
            sb := new(strings.Builder)
            for _, s := range v {
                sb.WriteString(strings.Title(s))
            }
            st = sb.String()
        }
    }
    func BenchmarkBuffer(b *testing.B) {
        for i := 0; i < b.N; i++ {
            p := new(bytes.Buffer)
            for _, s := range v {
                p.WriteString(strings.Title(s))
            }
            st = p.String()
        }
    }
    func BenchmarkJoin(b *testing.B) {
        for i := 0; i < b.N; i++ {
            x := make([]string, len(v))
            for i, s := range v {
                x[i] = strings.Title(s)
            }
            st = strings.Join(x, "")
        }
    }
    func BenchmarkReplaceAll(b *testing.B) {
        for i := 0; i < b.N; i++ {
            st = strings.ReplaceAll(strings.Title(strings.Join(v, " ")), " ", "")
        }
    }
    func BenchmarkLoop(b *testing.B) {
        for i := 0; i < b.N; i++ {
            n := 0
            for _, s := range v {
                n += len(s)
            }
            x := make([]rune, 0, n)
            for _, s := range v {
                for i, r := range s {
                    if i == 0 {
                        r = unicode.ToUpper(r)
                    }
                    x = append(x, r)
                }
            }
            st = string(x)
        }
    }
    
    var v = []string{"this", "is", "a", "sample", "string"}
    
    var st string
    
    命令:
    go test -v -bench=. -count=4 -benchmem -benchtime=1000000x
    
    输出:
    goos: linux
    goarch: amd64
    
    BenchmarkBuilder-8               1000000  809 ns/op  112 B/op  8 allocs/op
    BenchmarkBuilder-8               1000000  812 ns/op  112 B/op  8 allocs/op
    BenchmarkBuilder-8               1000000  753 ns/op  112 B/op  8 allocs/op
    BenchmarkBuilder-8               1000000  823 ns/op  112 B/op  8 allocs/op
    
    BenchmarkBuffer-8                1000000  845 ns/op  144 B/op  7 allocs/op
    BenchmarkBuffer-8                1000000  768 ns/op  144 B/op  7 allocs/op
    BenchmarkBuffer-8                1000000  805 ns/op  144 B/op  7 allocs/op
    BenchmarkBuffer-8                1000000  811 ns/op  144 B/op  7 allocs/op
    
    BenchmarkJoin-8                  1000000  930 ns/op  160 B/op  7 allocs/op
    BenchmarkJoin-8                  1000000  936 ns/op  160 B/op  7 allocs/op
    BenchmarkJoin-8                  1000000  896 ns/op  160 B/op  7 allocs/op
    BenchmarkJoin-8                  1000000  876 ns/op  160 B/op  7 allocs/op
    
    BenchmarkReplaceAll-8            1000000  793 ns/op  128 B/op  4 allocs/op
    BenchmarkReplaceAll-8            1000000  759 ns/op  128 B/op  4 allocs/op
    BenchmarkReplaceAll-8            1000000  765 ns/op  128 B/op  4 allocs/op
    BenchmarkReplaceAll-8            1000000  781 ns/op  128 B/op  4 allocs/op
    
    BenchmarkLoop-8                  1000000  535 ns/op  112 B/op  2 allocs/op
    BenchmarkLoop-8                  1000000  530 ns/op  112 B/op  2 allocs/op
    BenchmarkLoop-8                  1000000  506 ns/op  112 B/op  2 allocs/op
    BenchmarkLoop-8                  1000000  487 ns/op  112 B/op  2 allocs/op
    PASS
    ok       15.250s
    

    关于string - 转换大小写,然后加入字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/65164607/

    10-12 23:50