-
Notifications
You must be signed in to change notification settings - Fork 0
Strings in Perl 6
James E Keenan edited this page Feb 13, 2016
·
12 revisions
Basic information about strings
1 How are strings stored in P6?
Strings are stored in a Scalar and are enclosed in single or double quotes.
my $name = 'John Doe';
my $name2 = "James Doe";
say $name;
say $name2;
Output:
$
John Doe
James Doe
2 What are some functions available for strings?
- chars - will return the number of characters in string.
- flip - will return the same string in reverse.
- Case handling
- uc, lc - will uppercase, lowercase all characters in a string.
- fc - "fold case" converts all chars to a form suitable for case-less comparisons. This page discusses case folding's challenges.
- tc - "title case" upper-cases the 1st character in a string, leaving the rest.
- tclc - Uppercase first char, lowercase the rest
- wordcase - Uppercase each word. Can add rules, see S32 "Str" notes.
my $name = 'John Doe';
say $name.uc;
say $name.chars;
say $name.flip;
say 'Pass-Straße'.fc;
my $mixed_case='abC dEf ghI';
say 'tc=', $mixed_case.tc;
say 'tclc=', $mixed_case.tclc;
say 'wordcase=', $mixed_case.wordcase;
Output:
$
JOHN DOE
8
eoD nhoJ
pass-strasse
tc=AbC dEf ghI
tclc=Abc def ghi
wordcase=Abc Def Ghi
my $rv;
say 'chop():';
my $str1 = 'Pass-Straße';
say '$str1: ', $str1;
my $str2 = $str1;
$rv = chop($str2, 3);
say 'after chop 3 chars: ', $rv;
say '';
say 'chomp():';
my $line1 = "foobar\n";
say '$line1: ', "<$line1>";
$rv = chomp($line1);
say 'after chomp(): ', $rv;
my $line2 = "DOS\r\n";
say '$line2: ', "<$line2>";
$rv = chomp($line2);
say 'after chomp(): ', $rv;
say '';
say 'From http://doc.perl6.org/type/Str#method_unival:';
say "'unival' returns numeric value of first codepoint, or NaN if not numeric";
say "'75'.unival: ", '75'.unival;
say "'¾'.unival: ", '¾'.unival;
say "'A'.unival: ", 'A'.unival;
say "";
say "'univals' returns list of numeric values of codepoints; NaN if not numeric";
say "'75¾A'.univals: ", '75¾A'.univals;
say "";
say "perl6 has no 'length' function; normally we would use 'chars' instead";
say "usually, a grapheme is a character";
(http://doc.perl6.org/type/Str#method_length)
say 'DOS' ~ ' ', chars('DOS');
say 'DOS\n' ~ ' ', chars("DOS\n");
say 'DOS\r\n' ~ ' ', chars("DOS\r\n");
say '¾' ~ ' ', chars('¾');
say "";
say 'encode function returns Blob:';
say q|$str1: |, $str1;
say q|$str1.encode('UTF-8') |, $str1.encode('UTF-8');
say q|$str1.encode('UTF-16') |, $str1.encode('UTF-16');
say q|$str1.encode('ISO-8859-1') |, $str1.encode('ISO-8859-1');
Output:
chop():
$str1: Pass-Straße
after chop 3 chars: Pass-Str
chomp():
$line1: <foobar
>
after chomp(): foobar
$line2: <DOS
>
after chomp(): DOS
(http://doc.perl6.org/type/Str#method_unival)
'unival' returns numeric value of first codepoint, or NaN if not numeric
'75'.unival: 7
'¾'.unival: 0.75
'A'.unival: NaN
'univals' returns list of numeric values of codepoints; NaN if not numeric
'75¾A'.univals: (7 5 0.75 NaN)
perl6 has no 'length' function; normally we would use 'chars' instead
usually, a grapheme is a character
(http://doc.perl6.org/type/Str#method_length)
DOS 3
DOS\n 4
DOS\r\n 4
¾ 1
encode function returns Blob:
$str1: Pass-Straße
$str1.encode('UTF-8') utf8:0x<50 61 73 73 2d 53 74 72 61 c3 9f 65>
$str1.encode('UTF-16') utf16:0x<50 61 73 73 2d 53 74 72 61 df 65>
$str1.encode('ISO-8859-1') Blob[uint8]:0x<50 61 73 73 2d 53 74 72 61 df 65>
say 'index and rindex';
my $str = 'passacaglia and fugue';
say ' 012345678901234567890';
say '$str: ', $str;
say q|index $str, 'p': |, index $str, 'p';
say q|index $str, 'u': |, index $str, 'u';
say q|index $str, 'u', 5: |, index $str, 'u', 5;
say q|index $str, 'u', 18: |, index $str, 'u', 18;
say q|index $str, 'x': |, index $str, 'x';
say q|index($str, 'x').defined ?? 'OK' !! 'NOT OK': |, index($str, 'x').defined ?? 'OK' !! 'NOT OK';
say q|rindex $str, 'p': |, rindex $str, 'p';
say q|rindex $str, 'u': |, rindex $str, 'u';
say q|rindex $str, 'u', 5: |, rindex $str, 'u', 5;
say q|rindex $str, 'x': |, rindex $str, 'x';
Output:
index and rindex
012345678901234567890
$str: passacaglia and fugue
index $str, 'p': 0
index $str, 'u': 17
index $str, 'u', 5: 17
index $str, 'u', 18: 19
index $str, 'x': Nil
index($str, 'x').defined ?? 'OK' !! 'NOT OK': NOT OK
rindex $str, 'p': 0
rindex $str, 'u': 19
rindex $str, 'u', 5: Nil
rindex $str, 'x': Nil