String functions in Perl
The everyday string toolkit: case, length, search, slice, split and join.
use strict;
use warnings;
my $line = "the quick brown fox";
print uc($line), "\n";
print ucfirst($line), "\n";
print length($line), " chars\n";
print index($line, "brown"), "\n";
print substr($line, 4, 5), "\n";
my @words = split / /, $line;
print join("-", @words), "\n";
print "-" x 19, "\n";
How it works
ucshouts the whole line;ucfirstcapitalizes just the first letter.lengthcounts characters,indexfinds where a word starts,substrslices five out.splitbreaks the line into a word list andjoinglues it back with dashes.- The
xoperator repeats a string — a 19-dash ruler in five keystrokes.
Keywords and builtins used here
indexjoinlengthmyprintsplitsubstrucucfirstuse
The run, in numbers
- Lines
- 14
- Characters to type
- 297
- Tokens
- 91
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 73 seconds.
Step 1 of 3 in Strings, step 13 of 27 in Language basics.