Documentation for routine chars
assembled from the following pages:
Role: Blob §
From Blob
(Blob) method chars §
Defined as:
method chars(Blob:)
Throws X::Buf::AsStr
with chars
as payload.
Class: Str §
From Str
(Str) routine chars §
multi sub chars(Cool --> Int)multi sub chars(Str --> Int)multi sub chars(str --> int)multi method chars(Str: --> Int)
Returns the number of characters in the string in graphemes. On the JVM, this currently erroneously returns the number of codepoints instead.
Class: Cool §
From Cool
(Cool) routine chars §
Defined as:
multi sub chars(Cool )multi sub chars(Str )multi sub chars(str --> int)method chars(--> Int)
Coerces the invocant (or in sub form, its argument) to Str, and returns the number of characters in the string. Please note that on the JVM, you currently get codepoints instead of graphemes.
say 'møp'.chars; # OUTPUT: «3» say 'ã̷̠̬̊'.chars; # OUTPUT: «1» say '👨👩👧👦🏿'.chars; # OUTPUT: «1»
If the string is native, the number of chars will be also returned as a native int
.
Graphemes are user visible characters. That is, this is what the user thinks of as a “character”.
Graphemes can contain more than one codepoint. Typically the number of graphemes and codepoints differs when Prepend
or Extend
characters are involved (also known as Combining characters), but there are many other cases when this may happen. Another example is \c[ZWJ]
(Zero-width joiner).
You can check Grapheme_Cluster_Break
property of a character in order to see how it is going to behave:
say ‘ã̷̠̬̊’.uniprops(‘Grapheme_Cluster_Break’); # OUTPUT: «(Other Extend Extend Extend Extend)» say ‘👨👩👧👦🏿’.uniprops(‘Grapheme_Cluster_Break’); # OUTPUT: «(E_Base_GAZ ZWJ E_Base_GAZ ZWJ E_Base_GAZ ZWJ E_Base_GAZ E_Modifier)»
You can read more about graphemes in the Unicode Standard, which Raku tightly follows, using a method called NFG, normal form graphemes for efficiently representing them.
Class: Match §
From Match
(Match) method chars §
Defined as:
method chars()
Returns the numbers of characters in the matched string or 0 if there's been no match.
Returns the same as .Str.chars
.