Methods by type
samecase
FullRecase a string to follow the case pattern of another.
.samecase(pattern) rewrites a string's letters to match the upper/lower case pattern of a second string, position by position (the last case carries onward).
Matching a case pattern #
say "hello".samecase("AB");
Output
HELLOThe pattern "AB" is all uppercase, so hello becomes HELLO.
Following a mixed pattern #
The case is copied position by position, and the last case in the pattern carries on to the rest of the string — so "Ab" gives an initial capital then lowercase.
say "world".samecase("Ab");
say "WORLD".samecase("abc");
Output
World
world"Ab" title-cases (W upper, the rest lower); "abc" is all lowercase, so WORLD becomes world.
Notes #
- It's handy for preserving the casing of a word you're substituting — recase the replacement to
.samecasethe original. - Related case methods (
.uc,.lc,.tc,.tclc) are on the Case methods page;.samecasediffers by taking its casing from another string rather than a fixed rule. .indent(n)is a related layout method — it addsnleading spaces to each line (a negativenremoves indentation).