Is there a standard lib function which will return the count of characters in a string?

181 views
Skip to first unread message

T L

unread,
Jul 23, 2016, 10:52:29 AM7/23/16
to golang-nuts
len(str) will get the count of bytes.

utf8.RuneCountInString(str) will get the count of runes.

but how to get the count of characters?

Matt Silverlock

unread,
Jul 23, 2016, 11:04:44 AM7/23/16
to golang-nuts
https://blog.golang.org/strings

utf8.RuneCountInString is about as close as you will get. Do you have an example string that is not meeting expectations?

T L

unread,
Jul 23, 2016, 11:33:10 AM7/23/16
to golang-nuts


On Saturday, July 23, 2016 at 11:04:44 PM UTC+8, Matt Silverlock wrote:
https://blog.golang.org/strings

utf8.RuneCountInString is about as close as you will get. Do you have an example string that is not meeting expectations?

 
I forgot in which article (an official one) that says character != rune. And it provides an example, character é is made up of two runes, three bytes.

Tamás Gulácsi

unread,
Jul 23, 2016, 11:36:03 AM7/23/16
to golang-nuts
Yes, you have to normalize your String to be able to count the runes, and have that be the count of characters.

Jan Mercl

unread,
Jul 23, 2016, 11:54:07 AM7/23/16
to Tamás Gulácsi, golang-nuts

On Sat, Jul 23, 2016 at 5:36 PM Tamás Gulácsi <tgula...@gmail.com> wrote:

> Yes, you have to normalize your String to be able to count the runes, and have that be the count of characters.

In some scenario, where zero-width, half-width and full-width characters count all as any other character. IOW: OP should add what the count is to be used for.

--

-j

Manlio Perillo

unread,
Jul 23, 2016, 11:55:42 AM7/23/16
to golang-nuts
The term "character" may be used to refer to a "grapheme cluster".

How to find grapheme cluster boundaries is described in
but the algorithm is not (yet?) implemented in the golang.org/x/text package


Manlio 

T L

unread,
Jul 23, 2016, 1:50:18 PM7/23/16
to golang-nuts
Thanks for the information!

Lars Seipel

unread,
Jul 23, 2016, 5:31:31 PM7/23/16
to Tamás Gulácsi, golang-nuts
On Sat, Jul 23, 2016 at 08:36:03AM -0700, Tamás Gulácsi wrote:
> Yes, you have to normalize your String to be able to count the runes, and have that be the count of characters.

That would only work if there was a precomposed form for every possible
combining character sequence, which is not the case.

There are more issues. I would just give up on the concept of a
"character count". It's unlikely to be what you need anyway.
Reply all
Reply to author
Forward
0 new messages