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.

โ†‘ up