我如何使用GO中的字符串中找到重音字母?
给定一个代表一个单词的字符串 var word ,而代表字母的字符串 var字母,我如何计算单词中的重音字母的数量?
如果 var字母是一个非重音单词,我的代码有效,但是当字母重音或任何特殊字符时, var counter 打印了数字0。
package main
import "fmt"
func main() {
word := "cèòài"
letter := "è"
var counter int
for i := 0; i < len(word); i++ {
if string(word[i]) == letter {
counter++
}
}
fmt.Print(counter)
}
我想象此错误是由于一些编码问题,但不能完全掌握我需要研究的内容。
Given a string var word that represents a word, and a string var letter that represents a letter, how do I count the number of accented letters in the word?
If var letter is a non-accented word, my code works, but when the letter is accented or any special character, var counter prints the number 0.
package main
import "fmt"
func main() {
word := "cèòài"
letter := "è"
var counter int
for i := 0; i < len(word); i++ {
if string(word[i]) == letter {
counter++
}
}
fmt.Print(counter)
}
I imagine this error is due to some encoding problem but can't quite grasp what I need to look into.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何利用
strings。 count
:输出:
How about utilizing
strings.Count
:Output:
正如@jimb所暗示的那样,字符串中的字母(又称符文)可能不是完全匹配的字节:
在其单个符文上迭代字符串:
https://go.dev/play/p/wzoieedf-ee
As @JimB alluded to, letters (aka runes) in a string may not be at exactly matching byte-offsets:
To iterate a string over its individual runes:
https://go.dev/play/p/wZOIEedf-ee