字符串的元素具有字節類型,可以使用常規的索引操作進行訪問。我怎樣才能得到字符串的元素為char?“一些” [1]->“ o”
2 回答

慕勒3428872
TA貢獻1848條經驗 獲得超6個贊
最簡單的解決方案是將其轉換為符文數組:
var runes = []rune("someString")
請注意,當您迭代字符串時,不需要進行轉換。請參見Effective Go中的以下示例:
for pos, char := range "日本語" {
fmt.Printf("character %c starts at byte position %d\n", char, pos)
}
此打印
character 日 starts at byte position 0
character 本 starts at byte position 3
character 語 starts at byte position 6
- 2 回答
- 0 關注
- 213 瀏覽
添加回答
舉報
0/150
提交
取消