2 回答

TA貢獻1877條經(jīng)驗 獲得超1個贊
如何利用:strings.Count
package main
import (
"fmt"
"strings"
)
func main() {
word := "cèòài"
letter := "è"
letterOccurences := strings.Count(word, letter)
fmt.Printf("No. of occurences of \"%s\" in \"%s\": %d\n", letter, word, letterOccurences)
}
輸出:
No. of occurences of "è" in "cèòài": 1

TA貢獻1848條經(jīng)驗 獲得超10個贊
字符串中的字母(又名符文)可能與字節(jié)偏移量不完全匹配:
要在其各個符文上迭代一個字符串:
for pos, l := range word {
_ = pos // byte position e.g. 3rd rune may be at byte-position 6 because of multi-byte runes
if string(l) == letter {
counter++
}
}
https://go.dev/play/p/wZOIEedf-ee
- 2 回答
- 0 關(guān)注
- 128 瀏覽
添加回答
舉報