Mat*_*att 34 string loops go rune
我想这样:
for i := 0; i < len(str); i++ {
dosomethingwithrune(str[i]) // takes a rune
}
Run Code Online (Sandbox Code Playgroud)
但事实证明,它str[i]有type byte(uint8)而不是rune.
如何用符文而不是字节迭代字符串?
Den*_*ret 58
从Effective Go看到这个例子:
for pos, char := range "???" {
fmt.Printf("character %c starts at byte position %d\n", char, pos)
}
Run Code Online (Sandbox Code Playgroud)
这打印:
character ? starts at byte position 0
character ? starts at byte position 3
character ? starts at byte position 6
Run Code Online (Sandbox Code Playgroud)
对于字符串,该范围对您来说更有用,通过解析UTF-8来分解单个Unicode代码点.
小智 14
为了反映golang.org 上给出的示例,Go 允许您轻松地将字符串转换为符文切片,然后对其进行迭代,就像您最初想要的那样:
runes := []rune("Hello, ??")
for i := 0; i < len(runes) ; i++ {
fmt.Printf("Rune %v is '%c'\n", i, runes[i])
}
Run Code Online (Sandbox Code Playgroud)
当然,我们也可以像这里的其他示例一样使用范围运算符,但这更接近于您的原始语法。无论如何,这将输出:
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 '?'
Run Code Online (Sandbox Code Playgroud)
请注意,由于rune类型是 的别名int32,我们必须在语句中使用%c而不是通常%v的Printf,否则我们将看到 Unicode 代码点的整数表示(参见A Tour of Go)。
例如:
package main
import "fmt"
func main() {
for i, rune := range "Hello, ??" {
fmt.Printf("%d: %c\n", i, rune)
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
0: H
1: e
2: l
3: l
4: o
5: ,
6:
7: ?
10: ?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36725 次 |
| 最近记录: |