string 如何通过 Go 中的符文遍历字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18130859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I iterate over a string by runes in Go?
提问by Matt
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
.
但事实证明,它str[i]
具有类型byte
( uint8
) 而不是rune
。
How can I iterate over the string by runes rather than bytes?
如何通过符文而不是字节遍历字符串?
回答by Denys Séguret
See this example from Effective Go:
请参阅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.
对于字符串,范围为您做了更多的工作,通过解析 UTF-8 来分解单个 Unicode 代码点。
回答by zzzz
For example:
例如:
package main
import "fmt"
func main() {
for i, rune := range "Hello, 世界" {
fmt.Printf("%d: %c\n", i, rune)
}
}
Output:
输出:
0: H
1: e
2: l
3: l
4: o
5: ,
6:
7: 世
10: 界
回答by Justin Kulikauskas
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:
为了反映golang.org 上给出的示例,Go 允许您轻松地将字符串转换为符文切片,然后对其进行迭代,就像您最初想要的那样:
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).
请注意,由于rune
类型是 的别名int32
,因此我们必须在语句中使用%c
而不是通常%v
的类型Printf
,否则我们将看到 Unicode 代码点的整数表示(参见A Tour of Go)。