gpt4 book ai didi

string - 将字符串索引为字符

转载 作者:IT王子 更新时间:2023-10-29 01:16:50 26 4
gpt4 key购买 nike

The elements of strings have type byte and may be accessed using the usual indexing operations.

如何获取字符串中的元素作为字符?

"some"[1] -> "o"

最佳答案

最简单的解决方案是将其转换为 rune 数组:

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

关于string - 将字符串索引为字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com