Methods by type

Character translation — trans with ranges

Full

Map whole ranges of characters at once — upcasing, rot13, and the like.

.trans translates characters by a mapping. Beyond single characters (see String methods), the two sides can be ranges, so a whole alphabet maps in one go — the tr/// of other languages, generalised.

Range-to-range mapping #

say "hello".trans("a..z" => "A..Z");
say "abc".trans("abc" => "xyz");
Output
HELLO
xyz

"a..z" => "A..Z" maps each lowercase letter to its uppercase counterpart by position.

rot13 in one call #

Multiple ranges concatenate, so a single .trans can rotate both cases — the classic rot13.

say "Hello".trans("a..zA..Z" => "n..za..mN..ZA..M");
Output
Uryyb

Notes #