Logo

Programming-Idioms

History of Idiom 169 > diff from v2 to v3

Edit summary for version 3 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 2

2018-01-09, 17:04:38

Version 3

2018-01-09, 17:09:09

Idiom #169 String length

Assign to integer n the number of characters of string s.

This can be different from the number of bytes of s.

Idiom #169 String length

Assign to integer n the number of characters of string s.

This can be different from the number of bytes of s.

Extra Keywords
size
Extra Keywords
size
Imports
import "unicode/utf8"
Code
n := utf8.RuneCountInString(s)
Comments bubble
This assumes that s is encoded in UTF-8 (which is idiomatic in Go).
Doc URL
https://golang.org/pkg/unicode/utf8/#RuneCountInString
Demo URL
https://play.golang.org/p/mRaQKstRiTj