Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Clojure

Idiom #169 String length

Assign to the integer n the number of characters of the string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.

(defn n[s] (.codePointCount s 0 (count s)))

Here n is a function
(def n (.codePointCount s 0 (count s)))

This properly handles Unicode characters. Using count alone will not work for Unicode characters encoded in more than one byte.
n : Integer := s'Length;

New implementation...