go语言怎么遍历字符串

77次阅读
没有评论

共计 496 个字符,预计需要花费 2 分钟才能阅读完成。

在 Go 语言中,可以使用 range 关键字来遍历字符串。下面是一个简单的例子:

package main

import ("fmt"
)

func main() {str := "Hello, World!"

	for index, char := range str {fmt.Printf("Character at index %d is %c\n", index, char)
	}
}

在上面的例子中,我们使用 range 关键字遍历字符串 str,并输出每个字符的索引和字符本身。输出结果为:

Character at index 0 is H
Character at index 1 is e
Character at index 2 is l
Character at index 3 is l
Character at index 4 is o
Character at index 5 is ,
Character at index 6 is  
Character at index 7 is W
Character at index 8 is o
Character at index 9 is r
Character at index 10 is l
Character at index 11 is d
Character at index 12 is !

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-04-15发表,共计496字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)