Itโs Not Wrong that "๐คฆ๐ผโโ๏ธ".length == 7
last updated: 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.