Position in characters of a substring in Go

huangapple go评论110阅读模式
英文:

Position in characters of a substring in Go

问题

如何知道字符串中子字符串的位置,以字符(或符文)而不是字节为单位?

strings.Index(s, sub)会给出以字节为单位的位置。当使用Unicode时,它不匹配以符文为单位的位置:http://play.golang.org/p/DnlFjPaD2j

  1. func main() {
  2. s := "áéíóúÁÉÍÓÚ"
  3. fmt.Println(strings.Index(s, "ÍÓ"))
  4. }

结果:14。预期:7

当然,我可以将ssub转换为[]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

  1. func main() {
  2. s := "áéíóúÁÉÍÓÚ"
  3. fmt.Println(strings.Index(s, "ÍÓ"))
  4. }

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包后:

  1. func main() {
  2. s := "áéíóúÁÉÍÓÚ"
  3. i := strings.Index(s, "ÍÓ")
  4. fmt.Println(utf8.RuneCountInString(s[:i]))
  5. }

http://play.golang.org/p/Etszu3rbY3

英文:

You can do it like this, after importing the unicode/utf8 package:

  1. func main() {
  2. s := "áéíóúÁÉÍÓÚ"
  3. i := strings.Index(s, "ÍÓ")
  4. fmt.Println(utf8.RuneCountInString(s[:i]))
  5. }

http://play.golang.org/p/Etszu3rbY3

答案2

得分: 0

另一个选项:

  1. package main
  2. import "strings"
  3. func runeIndex(s, substr string) int {
  4. n := strings.Index(s, substr)
  5. if n == -1 { return -1 }
  6. r := []rune(s[:n])
  7. return len(r)
  8. }
  9. func main() {
  10. n := runeIndex("áéíóúÁÉÍÓÚ", "ÍÓ")
  11. println(n == 7)
  12. }
英文:

Another option:

  1. package main
  2. import "strings"
  3. func runeIndex(s, substr string) int {
  4. n := strings.Index(s, substr)
  5. if n == -1 { return -1 }
  6. r := []rune(s[:n])
  7. return len(r)
  8. }
  9. func main() {
  10. n := runeIndex("áéíóúÁÉÍÓÚ", "ÍÓ")
  11. println(n == 7)
  12. }

huangapple
  • 本文由 发表于 2014年2月16日 17:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/21809653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定