Go的[]byte与string
文章目录
1 | package main |
byte 对应 utf-8 英文占1个字节,中文占3个字节
rune 对应 unicode 英文字符占2个字节,中文字符占2个字节
在Go中字符串是不可变的,例如下面的代码编译时会报错:
var s string = "hello" s[0] = 'c'
但如果真的想要修改怎么办呢?下面的代码可以实现:
s := "hello"
c := []byte(s) // 将字符串 s 转换为 []byte 类型 c[0] = 'c'
s2 := string(c) // 再转换回 string 类型 fmt.Printf("%s\n", s2)
Go中可以使用+操作符来连接两个字符串:
s := "hello,"
m := " world"
a := s + m fmt.Printf("%s\n", a)
1
2
3
修改字符串也可写为:
```go
s := "hello"
s = "c" + s[1:] // 字符串虽不能更改,但可进行切片操作 fmt.Printf("%s\n", s)