将一个字节追加到字符串中。

huangapple go评论120阅读模式
英文:

Append a byte to a string?

问题

在Go语言中,将字节追加到字符串的方法如下:

  1. var ret string
  2. var b byte
  3. ret += string(b)

但是,你提供的代码会导致错误,因为字符串和字节类型不匹配。正确的做法是将字节转换为字符串后再进行追加操作。

英文:

How do you append a byte to a string in Go?

  1. var ret string
  2. var b byte
  3. ret += b
  4. invalid operation: ret += b (mismatched types string and byte)

答案1

得分: 9

以下是翻译好的内容:

这里有几个选项:

// 作为切片追加字节
ret += string([]byte{b})

// 作为符文追加字节
ret += string(rune(b))

// 将字符串转换为字节切片,追加字节到切片,再转换回字符串
ret = string(append([]byte(ret), b))

进行基准测试以确定哪个选项最好。

如果您想追加多个字节,则将第二个选项拆分为多个语句,并追加到[]byte中:

buf := []byte(ret) // 将字符串转换为字节切片
buf = append(buf, b) // 追加字节到切片
buf = append(buf, b1) // 追加字节到切片
... 等等
ret = string(buf) // 转换回字符串

如果您想追加符文r,则稍微简单一些:

ret += string(r)

字符串是不可变的。上面的代码创建了一个新的字符串,它是原始字符串和一个字节或符文的连接。

英文:

Here are a few options:

  1. // append byte as slice
  2. ret += string([]byte{b})
  3. // append byte as rune
  4. ret += string(rune(b))
  5. // convert string to byte slice, append byte to slice, convert back to string
  6. ret = string(append([]byte(ret), b))

Benchmark to see which one is best.

If you want to append more than one byte, then break the second option into multiple statements and append to the []byte:

  1. buf := []byte(ret) // convert string to byte slice
  2. buf = append(buf, b) // append byte to slice
  3. buf = append(buf, b1) // append byte to slice
  4. ... etc
  5. ret = string(buf) // convert back to string

If you want to append the rune r, then it's a little simpler:

  1. ret += string(r)

Strings are immutable. The code above creates a new string that is a concatenation of the original string and a byte or rune.

答案2

得分: 9

除了ThunderCats的回答之外,你还可以从字符串初始化一个bytes.Buffer,这样你就可以根据需要继续追加字节:

  1. buff := bytes.NewBufferString(ret)
  2. // 如果性能有问题,可以使用 buff.Grow(n)
  3. buff.WriteByte(b)
  4. buff.WriteByte(b)
  5. // ...
  6. result := buff.String()
英文:

In addition to ThunderCats answer.. you could initialize a bytes.Buffer from a string ... allowing you to continue appending bytes as you see fit:

  1. buff := bytes.NewBufferString(ret)
  2. // maybe buff.Grow(n) .. if you hit perf issues?
  3. buff.WriteByte(b)
  4. buff.WriteByte(b)
  5. // ...
  6. result := buff.String()

答案3

得分: 5

这是比其他答案都简单得多的方法:

  1. var ret string = "test"
  2. var b byte = 'a'
  3. ret += string(b)
  4. // 返回 "testa"

也就是说,你可以将整数强制转换为字符串,它会将整数视为符文(byte 是整数类型)。然后你可以使用 + 将得到的字符串连接起来。

Playground: https://play.golang.org/p/ktnUg70M-I

英文:

It's a lot simpler than either of the other answers:

  1. var ret string = "test"
  2. var b byte = 'a'
  3. ret += string(b)
  4. // returns "testa"

That is, you can just cast an integer to a string and it will treat the integer as a rune (byte is an integer type). An then you can just concatenate the resulting string with +

Playground: https://play.golang.org/p/ktnUg70M-I

答案4

得分: 0

另一种在Golang中的解决方案。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. byteArr := []byte{1,2,5,4}
  7. stringStr := "Test"
  8. output:= fmt.Sprintf("%v %v",byteArr,stringStr)
  9. fmt.Println("Output: ",output)
  10. }

输出

Output: [1 2 5 4] Test

英文:

Another solution in Golang.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. byteArr := []byte{1,2,5,4}
  7. stringStr := "Test"
  8. output:= fmt.Sprintf("%v %v",byteArr,stringStr)
  9. fmt.Println("Output: ",output)
  10. }

> Output

  1. Output: [1 2 5 4] Test

答案5

得分: 0

你也可以使用strings.Builder

  1. package main
  2. import "strings"
  3. func main() {
  4. var b strings.Builder
  5. b.WriteByte('!')
  6. println(b.String() == "!")
  7. }

https://golang.org/pkg/strings#Builder.WriteByte

英文:

You can also use strings.Builder:

  1. package main
  2. import "strings"
  3. func main() {
  4. var b strings.Builder
  5. b.WriteByte('!')
  6. println(b.String() == "!")
  7. }

https://golang.org/pkg/strings#Builder.WriteByte

huangapple
  • 本文由 发表于 2015年3月16日 10:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/29068939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定