utf8.RuneCountInString, utf8.RuneCount
func RuneCountInString(s string) (n int)
func RuneCount(p []byte) intCount Unicode code points in a string or byte slice, which is not the same as counting glyphs.
| Parameter | Description |
|---|---|
s / p | A string or a slice of UTF-8 bytes. Invalid sequences still contribute one rune per bad byte. |
Returns: The number of runes, including U+FFFD-width counts for invalid bytes as documented for RuneCount.
- len(s) remains a byte count, as already shown on the strings page.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
composed := "é"
decomposed := "é"
fmt.Println(utf8.RuneCountInString(composed), len(composed), utf8.RuneCountInString(decomposed), len(decomposed))
fmt.Println(composed == decomposed, utf8.RuneCountInString("👍"), len("👍"))
}Expected output
1 2 2 3
false 1 4Related: strings