Methods by type
Trimming — trim / chomp / chop
FullRemove 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
helNotes #
.chomponly removes a newline, and only one — it's the right tool for cleaning a line read from input, where.trimmight strip meaningful spaces..chopremoves characters regardless of what they are;.chop(n)removesn.- All of these return a new string; the original is unchanged (use
.=chompetc. to mutate in place).