Newbie question: the best way to get the character count from a string?

Hi there,

Sorry if my question is dumb, I am just learning golang.

I noticed if s is a string, len(s) return the length of utf-8 byte representation of string s, not the number of characters of s. I tried with (1) and (2) and both works for me, but it seems both has big memory footprint if s is long, is there a function that can do the same thing with minimum memory footprint?

(1) len([]rune(s))
(2) utf8.RuneCount([]byte(s))

I don’t know about the memory footprint, but this is another way.

Just found there is an api RuneCountInString which does exactly what I want. Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.