Position in characters of a substring in Go

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

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

当然,我可以将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

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]))
}

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

答案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)
}

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:

确定