Real length of string

1,451 views
Skip to first unread message

adrianopol

unread,
Mar 13, 2012, 11:16:46 AM3/13/12
to golang-nuts
Hello, everyone.

How can I calculate the number of characters in a string, consisting
of ascii (1 byte) and cyrillic (2 bytes) characters? Function len()
tells me only the number of bytes. And a more general question which I
also have not found answer to: why a language, intended to be actively
used for web programming, has such uneasy way to deal with strings
containing multibyte symbols (compared to Java, for example)?

roger peppe

unread,
Mar 13, 2012, 12:32:18 PM3/13/12
to adrianopol, golang-nuts
On 13 March 2012 15:16, adrianopol <adria...@gmail.com> wrote:
> Hello, everyone.
>
> How can I calculate the number of characters in a string, consisting
> of ascii (1 byte) and cyrillic (2 bytes) characters? Function len()
> tells me only the number of bytes.

strings.RuneCountInString is probably what you want.

> And a more general question which I
> also have not found answer to: why a language, intended to be actively
> used for web programming, has such uneasy way to deal with strings
> containing multibyte symbols (compared to Java, for example)?

the concept of a "character" is a slippery one in the presence of
combining characters. one person's answer might not be another's.

i find Go works pretty well with multibyte symbols. it's relatively
rare to need random access to characters in my experience.

Tarmigan

unread,
Mar 13, 2012, 12:39:17 PM3/13/12
to adrianopol, golang-nuts

Go works very well with utf-8:
http://play.golang.org/p/H5urxBSgPp

-Tarmigan

adria...@gmail.com

unread,
Mar 13, 2012, 12:40:06 PM3/13/12
to golan...@googlegroups.com, adrianopol


On Tuesday, March 13, 2012 8:32:18 PM UTC+4, rog wrote:

strings.RuneCountInString is probably what you want.

Thank you, but the package is utf8: http://golang.org/pkg/utf8/#RuneCountInString.

adrianopol

unread,
Mar 13, 2012, 2:06:48 PM3/13/12
to golan...@googlegroups.com
Thank you very much!
Reply all
Reply to author
Forward
0 new messages