How can I iterate over a string by runes in Go?

笑着哭i 提交于 2021-02-17 11:11:01

问题


I wanted to this:

for i := 0; i < len(str); i++ {
    dosomethingwithrune(str[i]) // takes a rune
}

But it turns out that str[i] has type byte (uint8) rather than rune.

How can I iterate over the string by runes rather than bytes?


回答1:


See this example from Effective Go :

for pos, char := range "日本語" {
    fmt.Printf("character %c starts at byte position %d\n", char, pos)
}

This prints :

character 日 starts at byte position 0
character 本 starts at byte position 3
character 語 starts at byte position 6

For strings, the range does more work for you, breaking out individual Unicode code points by parsing the UTF-8.




回答2:


For example:

package main

import "fmt"

func main() {
        for i, rune := range "Hello, 世界" {
                fmt.Printf("%d: %c\n", i, rune)
        }
}

Playground


Output:

0: H
1: e
2: l
3: l
4: o
5: ,
6:  
7: 世
10: 界



回答3:


To mirror an example given at golang.org, Go allows you to easily convert a string to a slice of runes and then iterate over that, just like you wanted to originally:

runes := []rune("Hello, 世界")
for i := 0; i < len(runes) ; i++ {
    fmt.Printf("Rune %v is '%c'\n", i, runes[i])
}

Of course, we could also use a range operator like in the other examples here, but this more closely follows your original syntax. In any case, this will output:

Rune 0 is 'H'
Rune 1 is 'e'
Rune 2 is 'l'
Rune 3 is 'l'
Rune 4 is 'o'
Rune 5 is ','
Rune 6 is ' '
Rune 7 is '世'
Rune 8 is '界'

Note that since the rune type is an alias for int32, we must use %c instead of the usual %v in the Printf statement, or we will see the integer representation of the Unicode code point (see A Tour of Go).



来源:https://stackoverflow.com/questions/18130859/how-can-i-iterate-over-a-string-by-runes-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!