Documentation for method trans

Documentation for method trans, assembled from the following types:

class Str

From Str

(Str) method trans

multi method trans(Str:D: Pair:D \what*%n --> Str)
multi method trans(Str:D: *@changes:complement(:$c), :squash(:$s), :delete(:$d--> Str)

Replaces one or many characters with one or many characters. Ranges are supported, both for keys and values. Regexes work as keys. In case a list of keys and values is used, substrings can be replaced as well. When called with :complement anything but the matched value or range is replaced with a single value. With :delete the matched characters are removed. Combining :complement and :delete will remove anything but the matched values. The adverb :squash will reduce repeated matched characters to a single character.

Example:

my $str = 'say $x<b> && $y<a>';
$str.=trans'<' => '«' );
$str.=trans'<' => '«''>' => '»' );
 
$str.=trans( [ '<'   , '>'   , '&' ] =>
             [ '&lt;''&gt;''&amp;' ]);
 
$str.=trans( ['a'..'y'=> ['A'..'z'] );
 
"abcdefghij".trans(/<[aeiou]> \w/ => '');                     # RESULT: «cdgh» 
 
"a123b123c".trans(['a'..'z'=> 'x':complement);            # RESULT: «axxxbxxxc» 
"a123b123c".trans('23' => '':delete);                       # RESULT: «a1b1c» 
"aaa1123bb123c".trans('a'..'z' => 'A'..'Z':squash);         # RESULT: «A1123B123C» 
"aaa1123bb123c".trans('a'..'z' => 'x':complement:squash); # RESULT: «aaaxbbxc»