golang 基础(19)字符串
来源:zidea     阅读:541
源码超市
发布于 2019-03-26 01:50
查看主页
square-gopher.png

字符和字符串解决

func main(){    s := "Yes我爱Go!"    fmt.Printf("%s\n",[]byte(s))}

这样打印出是就是 "Yes我爱Go!"、打印一下(fmt.Println(len(s)))输出 s 长度为 12 。那么这个 12 是怎样来的呢。

fmt.Printf("%X\n",[]byte(s))

可以打印出里面具体字节的数字,会使用 %X 来打印字节

596573E68891E788B1476F21

这样并不便于观察,我们可以通过 for 循环来讲字符一个一个打印出来

for _, b := range []byte(s){            fmt.Printf("%X ",b)}fmt.Println()
59      65      73      E6      88      91      E7      88      B1      47      6F      21

这个是 ASC码,( E6 88 91)每个中文占三个字节,这个就是 utf-8 编码,是一个可变长的编码形式,那么每一个英文字母占一个字节。

上面我们是看字符串的字节是怎样放置的

我们换一种方式进行遍历打印,打印出没个字符的 unicode

for i, ch := range s{        fmt.Printf("(%d %X) ", i,ch)    }

从索引 3 开始值是 6211 ch 是一个 int32 类型,也就是 rune,rune 就是 int32 的别名 UTF-8 转 unicode,E6 88 91 是 uft-8 的编码转成了 unicode 编码 6211,下一字是从 6 开始转为 unicode 7231。这样打印出来索引并不连续。

(0 59) (1 65) (2 73) (3 6211) (6 7231) (9 47) (10 6F) (11 21)

可以使用 go 语言 utf8 标准库提供的 RuneCountInString 来进行打印。

fmt.Println("Rune count: ", utf8.RuneCountInString(s))

使用 RuneCountInString 这回输出为 9 对了

Rune count:  9

这样我们即可以通过遍历一个一个打印出来字符

bytes := []byte(s)    //拿到字节    for len(bytes) > 0 {        ch, size := utf8.DecodeRune(bytes)        //字符 中文字符size 3        bytes = bytes[size:]        fmt.Printf("%c ", ch)    }
package mainimport (    "fmt"    "unicode/utf8")func main(){    s := "Yes我爱Go!"    fmt.Println(len(s)) // 12    fmt.Println(s)    fmt.Printf("%X\n",[]byte(s))//596573E68891E788B1476F21    for _, b := range []byte(s){                fmt.Printf("%X\t",b)    }    //59      65      73      E6      88      91      E7      88      B1      47      6F      21    //每个中文是 3 字节 3 * 2 + 6 = 12 字节    fmt.Println()    for i, ch := range s{        fmt.Printf("(%d %X) ", i,ch)    }    //(0 59) (1 65) (2 73) (3 6211) (6 7231) (9 47) (10 6F) (11 21)    // 3 6211 ch 本身是 int 32 那就是 rune unicode 编码    //(6) utf-8 进行解码为 unicode     fmt.Println()    fmt.Println("Rune count: ", utf8.RuneCountInString(s))    //9 这个对了    bytes := []byte(s)    //拿到字节    for len(bytes) > 0 {        ch, size := utf8.DecodeRune(bytes)        //字符 中文字符size 3        bytes = bytes[size:]        fmt.Printf("%c ", ch)    }    //我们    fmt.Println()    for i,ch := range []rune(s) {        fmt.Printf("(%d %c)",i,ch)        //(0 Y)(1 e)(2 s)(3 我)(4 爱)(5 G)(6 o)(7 !)    }    //转为 rune 数组或者 slice    //range 遍历 pos rune 对 pos 是不连续    // utf8 RuneCountInString 获取字符数    // 使用len取得字节长度    // 使用 byte 获取字节    }
highres_442654178.jpeg
免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境 服务器应用
相关推荐
C++11(及现代C++风格)和快速迭代式开发
Android视频开发进阶-关于视频的那些术语
前台面试每日 3+1 —— 第491天
【JavaWeb】103:客户注册功能的实现
如何帮助前台web新人入门和提高?
首页
搜索
订单
购物车
我的