Strings and Unicode

5.1 Quoting

my $lang = 'Raku';
say 'single: no $interpolation';
say "double: $lang, {1 + 1}, @(1, 2), method call: $lang.uc()";
say "email: user\@example.com";            # escape @ when followed by a name-like char
say q{same as single quotes};
say qq{same as double: $lang};
say Q{completely raw: \n $lang};
say qw<a b c>.raku;                        # ("a", "b", "c")
say <a b 42>.raku;                         # ("a", "b", IntStr.new(42, "42")) allomorphs
my $heredoc = q:to/END/;
    Indented heredoc:
    indentation of the closing marker is removed.
    END
say $heredoc;
say qq:to/EOT/;
    Hello, $lang!
    EOT

5.2 String Operations

my $s = "Hello, Raku World";
say $s.chars;                     # 17 (graphemes)
say $s.uc, ' ', $s.lc, ' ', $s.tc, ' ', $s.fc;     # upper, lower, title-case first char, fold case
say $s.wordcase;                  # Hello, Raku World
say $s.flip;                      # reverse
say $s.substr(7, 4);              # Raku
say $s.index('Raku'), ' ', $s.rindex('o');
say $s.contains('Raku'), ' ', $s.starts-with('He'), ' ', $s.ends-with('ld');
say $s.split(', ');               # (Hello Raku World)
say $s.words;                     # (Hello, Raku World)
say $s.comb;                      # all characters
say $s.comb(/\w+/);               # (Hello Raku World)
say $s.subst('World', 'Universe');
say $s.trans('lo' => 'LO');       # transliteration
say '  padded  '.trim, '|', '  x'.trim-leading, '|';
say 'ab' x 3;                     # ababab
say 'Hello' ~ ', ' ~ 'World';     # ~ concatenates
say 'abc' lt 'abd', ' ', 'a' leg 'b';   # True Less
say $s.lines.elems, ' ', "a\nb\nc".lines;
say sprintf('%-10s|%5.2f|%04d', 'name', π, 42);
say 42.fmt('%08b'), ' ', (1, 2, 3).fmt('%02d', ', ');
say 'raku' ~~ /ak/ ?? 'match' !! 'no';
say $s.ords.head(3), ' ', 72.chr;
say "Tab\tNewline\n".raku;
say 'abc'.succ
…

5.3 Unicode: Graphemes, Codes and Bytes

Raku strings use NFG (Normal Form Grapheme): each user-perceived character is one unit, so indexing and length are grapheme-correct by default.

my $family = "👨‍👩‍👧";                # one grapheme made of 5 code points
say $family.chars;                # 1
say $family.codes;                # 5
say $family.encode('utf8').bytes; # 18
say "e\x[301]" eq "é";            # True (NFG normalizes)
say "\c[GREEK SMALL LETTER ALPHA]", ' ', "\x[263A]";
say 'ß'.uc, ' ', 'dž'.tc;          # SS Dž
say 'café'.NFD.list.elems;        # 5 code points decomposed
say '٣'.Int;                      # 3 (Unicode digits are numeric)
say 'Raku'.uniname, ' ', '€'.uniprop;   # LATIN CAPITAL LETTER R  Sc
say "日本語".chars;                # 3
my $bytes = "héllo".encode('UTF-8');     # Blob
say $bytes.decode('UTF-8');