Logo

Programming-Idioms

  • D
  • Smalltalk
  • Ada

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.

size_t n = s.length;

n is the number of UTF-8 code units.
import std.uni, std.range;
size_t n = s.byGrapheme.walkLength;

n is the count of graphic characters.
std.range;
size_t n = s.walkLength;

n is the count of UTF-8 code points.
n := s size.
n : Integer := s'Length;
(defn n[s] (.codePointCount s 0 (count s)))

Here n is a function

New implementation...