stringgorune

Position in characters of a substring in Go


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?


Solution

  • 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