前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang-101-hacks(10)——String

golang-101-hacks(10)——String

作者头像
羊羽shine
发布2019-05-31 14:21:09
4840
发布2019-05-31 14:21:09
举报
文章被收录于专栏:Golang开发Golang开发

注:本文是对golang-101-hacks中文翻译。 在Go中string是由不可变的字节数组构成的。一旦赋值,就不能修改字符串的值。例如 In Go, string is an immutable array of bytes. So if created, we can't change its value. E.g.:

代码语言:javascript
复制
package main

func main()  {
    s := "Hello"
    s[0] = 'h'
}

编译结果会提示错误: The compiler will complain:

代码语言:javascript
复制
cannot assign to s[0] 

对字符串的修改可以将其转换为“byte”数组。但是实际上您并没有对原始字符串进行修改操作,改变的只是一个原始字符串的副本。

To modify the content of a string, you could convert it to a byte array. But in fact, you do not operate on the original string, just a copy:

代码语言:javascript
复制
package main

import "fmt"

func main()  {
    s := "Hello"
    b := []byte(s)
    b[0] = 'h'
    fmt.Printf("%s\n", b)
} 

运行结果 The result is like this:

代码语言:javascript
复制
hello

Go使用的是UTF-8的编码方式,函数len的结果是当前字符字节的长度而不是字符长度 Since Go uses UTF-8 encoding, you must remember the len function will return the string's byte number, not character number:

代码语言:javascript
复制
package main

import "fmt"

func main()  {
    s := "日志log"
    fmt.Println(len(s))
} 

执行结果是 The result is:

代码语言:javascript
复制
9

由于每个中文占3个字节,上面的例子中s字符串有5个字符和9个字节组成的 Because each Chinese character occupied 3 bytes, s in the above example contains 5 characters and 9 bytes. 对字符串执行for ... range循环语句可以获取到每个字符 If you want to access every character, for ... range loop can give a help:

代码语言:javascript
复制
package main
import "fmt"

func main() {
    s := "日志log"
    for index, runeValue := range s {
        fmt.Printf("%#U starts at byte position %d\n", runeValue, index)
    }
}

执行结果

代码语言:javascript
复制
U+65E5 '日' starts at byte position 0
U+5FD7 '志' starts at byte position 3
U+006C 'l' starts at byte position 6
U+006F 'o' starts at byte position 7
U+0067 'g' starts at byte position 8
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档