string length

Gives the count of characters of a string.

string length STRING
string length -grapheme STRING

Description

Gives the count of characters (code points) of STRING. An empty string has length 0. A character outside the Basic Multilingual Plane, such as an emoji, counts as one character.

With -grapheme it gives the count of grapheme clusters. With one argument, string length -grapheme is the length of the text -grapheme, so the option needs a string after it.

Arguments

ArgumentTypeDefaultDescription
STRINGstringrequiredThe text to count.

Options

OptionTypeDefaultSummary
-graphemeflagnoneCounts grapheme clusters in place of characters.

-grapheme

Counts grapheme clusters in place of characters.

Counts the grapheme clusters of STRING with the rules of Unicode text segmentation. A base letter and its combining marks are one cluster, a flag of two regional indicators is one cluster, and an emoji sequence joined with U+200D is one cluster. An empty string has 0 clusters.

The option has no prefix form: -g or -graph is an error of the count of arguments.

% string length -grapheme "é"
1

# A family emoji: three code points joined by two U+200D.
% string length "\U1F468‍\U1F469‍\U1F467"
5

% string length -grapheme "\U1F468‍\U1F469‍\U1F467"
1
% string length -g abc
wrong # args: should be "string length string"
-errorcode TCL WRONGARGS

Returns

A non-negative integer.

Examples

% string length "buy milk"
8

% string length ""
0

# With one argument, -grapheme is the string.
% string length -grapheme
9

Errors

% string length a b
wrong # args: should be "string length string"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also