Methods by type

Trimming — trim / chomp / chop

Full

Remove whitespace or characters from the ends of a string.

Several methods remove characters from a string's ends: trim (and its one-sided trim-leading/trim-trailing) strip whitespace, chomp removes a trailing newline, and chop removes trailing characters.

One-sided trimming #

.trim-leading strips leading whitespace; .trim-trailing strips trailing. (.trim does both — see String methods.)

say "  hi  ".trim-leading ~ "|";
say "  hi  ".trim-trailing ~ "|";
Output
hi  |
  hi|

chomp and chop #

.chomp removes a single trailing newline (if present); .chop removes the last character (or the last n with an argument).

say "line\n".chomp;
say "hello".chop;
say "hello".chop(2);
Output
line
hell
hel

Notes #