Logo

Programming-Idioms

  • JS
  • Elixir
  • Java

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.

const n = s.length;
const n = [...s].length;

Sets n to the count of characters in s, not the number of bytes.
n = String.length s
int n = s.length();

This works only if s != null.

If s == null, this throws a NullPointerException.
n : Integer := s'Length;

New implementation...