hic003cih / Golang

0 stars 0 forks source link

解決國際字符問題(rune) #31

Open hic003cih opened 4 years ago

hic003cih commented 4 years ago

rune相当于 go 语言的 char

//func DecodeRune(p []byte) (r rune, size int) ch, size := utf8.DecodeRune(bytes)

s := "Yes我爱慕课网!"

使用range遍歷 pos,rune

使用utf8.RuneCountInString去獲得字符數量 fmt.Println(utf8.RuneCountInString(s)) 得到值->9 使用 len 获得字节长度 len(bytes) UTF-8 中文占三字节 所以 s := "Yes我爱慕课网!" , 長度是19

使用[]byte 获得字节 可以使用 []byte 將每個字都換成bytes s := "Yes我爱慕课网!" bytes := []byte(s) 輸出 -> [89 101 115 230 136 145 231 136 177 230 133 149 232 175 190 231 189 145 33]