」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 我如何通過符文在GO中迭代字符串?

我如何通過符文在GO中迭代字符串?

發佈於2025-03-24
瀏覽:498

How Do I Iterate Over Strings by Runes in Go? 
通过在go

迭代时要迭代过高,而在尝试使用索引上迭代字符串时,您可能会遇到一个问题,您可能会遇到一个问题,而str [i]返回byte而不是跑步。这是因为GO中的字符串是字节的序列,而不是runs。

通过runes迭代字符串,使用范围关键字。例如: for POS,char:= range“日本语” { fmt.printf(“字符%c从字节位置%d \ n开始”,char,pos) } [2 字符本从字节位置3开始 character 語 starts at byte position 6

The range syntax does the following:

Iterates from 0 to the length of the string

for pos, char := range "日本語" {
    fmt.Printf("character %c starts at byte position %d\n", char, pos)
}

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3