Itβs Not Wrong that "π€¦πΌββοΈ".length == 7
Oct 20, 2023
https://hsivonen.fi/string-length
But Itβs Better that "π€¦πΌββοΈ".len() == 17 and Rather Useless that len("π€¦πΌββοΈ") == 5
After reading this article, I wanted to see how go handled the titular question:
package main
import (
"fmt"
"unicode/utf8"
"github.com/rivo/uniseg"
)
var s = "π€¦πΌββοΈ"
func main() {
fmt.Printf("length of string %s with various tools\n\n, s)
fmt.Printf("len(): %d\n", len(s))
fmt.Printf("utf8.runeCountInString: %d\n", utf8.RuneCountInString(s))
fmt.Printf("len([]rune()): %d\n", len([]rune(s)))
fmt.Printf("rivo/uniseg.GraphemeClusterCount: %d\n", uniseg.GraphemeClusterCount(s))
}
$ ./runetest
length of string π€¦πΌββοΈ with various tools
len(): 17
utf8.runeCountInString: 5
len([]rune()): 5
rivo/uniseg.GraphemeClusterCount: 1
akkartik kindly pointed me to libgrapheme, which looks cool.