routine chars
1 | class Str |
1.1 | (Str) routine chars |
2 | class Match |
2.1 | (Match) method chars |
3 | role Blob |
3.1 | (Blob) method chars |
4 | class Cool |
4.1 | (Cool) routine chars |
Documentation for routine chars
assembled from the following types:
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 Match
From Match
(Match) method chars
Returns the numbers of characters in the matched string.
Returns the same as .Str.chars
.
role Blob
From Blob
(Blob) method chars
Defined as:
method chars(Blob:)
Throws X::Buf::AsStr
with chars
as payload.
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.