Logo

Programming-Idioms

History of Idiom 169 > diff from v59 to v60

Edit summary for version 60 by el_memorioso:
[Clojure] Fixed problem with Unicode character encoded in more than one byte.

Version 59

2019-09-26, 17:25:24

Version 60

2019-09-26, 17:33:57

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.

Illustration

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.

Illustration
Extra Keywords
size characters chars number runes
Extra Keywords
size characters chars number runes
Code
(def n (count s))
Code
(def n (.codePointCount s 0 (count s)))
Comments bubble
This properly handles Unicode characters. Using count alone will not work for Unicode characters encoded in more than one byte.