44

golang 基础(19)字符串

 5 years ago
source link: https://studygolang.com/articles/19220?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
U3IrMnj.png!web

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 main

import (
    "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 获取字节
    
}
aA7FNfN.png!web

highres_442654178.jpeg


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK