英文:
Position in characters of a substring in Go
问题
如何知道字符串中子字符串的位置,以字符(或符文)而不是字节为单位?
strings.Index(s, sub)
会给出以字节为单位的位置。当使用Unicode时,它不匹配以符文为单位的位置:http://play.golang.org/p/DnlFjPaD2j
func main() {
s := "áéíóúÁÉÍÓÚ"
fmt.Println(strings.Index(s, "ÍÓ"))
}
结果:14。预期:7
当然,我可以将s
和sub
转换为[]rune
,然后手动查找子切片,但是否有更好的方法来做到这一点?
与此相关的是,要获取字符串的前n
个字符,我正在使用以下方法:string([]rune(s)[:n])
。这是最好的方法吗?
英文:
How can I know the position of a substring in a string, in characteres (or runes) instead of bytes?
strings.Index(s, sub)
will give the position in bytes. When using Unicode, it doesn't match the position in runes: http://play.golang.org/p/DnlFjPaD2j
func main() {
s := "áéíóúÁÉÍÓÚ"
fmt.Println(strings.Index(s, "ÍÓ"))
}
Result: 14. Expected: 7
Of course, I could convert s
and sub
to []rune
and look for the subslice manually, but is there a better way to do it?
Related to this, to get the first n
characters of a string I'm doing this: string([]rune(s)[:n])
. Is it the best way?
答案1
得分: 11
你可以这样做,导入unicode/utf8
包后:
func main() {
s := "áéíóúÁÉÍÓÚ"
i := strings.Index(s, "ÍÓ")
fmt.Println(utf8.RuneCountInString(s[:i]))
}
http://play.golang.org/p/Etszu3rbY3
英文:
You can do it like this, after importing the unicode/utf8
package:
func main() {
s := "áéíóúÁÉÍÓÚ"
i := strings.Index(s, "ÍÓ")
fmt.Println(utf8.RuneCountInString(s[:i]))
}
答案2
得分: 0
另一个选项:
package main
import "strings"
func runeIndex(s, substr string) int {
n := strings.Index(s, substr)
if n == -1 { return -1 }
r := []rune(s[:n])
return len(r)
}
func main() {
n := runeIndex("áéíóúÁÉÍÓÚ", "ÍÓ")
println(n == 7)
}
英文:
Another option:
package main
import "strings"
func runeIndex(s, substr string) int {
n := strings.Index(s, substr)
if n == -1 { return -1 }
r := []rune(s[:n])
return len(r)
}
func main() {
n := runeIndex("áéíóúÁÉÍÓÚ", "ÍÓ")
println(n == 7)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论