typestar

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

  1. uc shouts the whole line; ucfirst capitalizes just the first letter.
  2. length counts characters, index finds where a word starts, substr slices five out.
  3. split breaks the line into a word list and join glues it back with dashes.
  4. The x operator repeats a string — a 19-dash ruler in five keystrokes.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Strings, step 13 of 27 in Language basics.

← Previous Next →

String functions in other languages