Skip to content

Commit 4a4055e

Browse files
committed
Merge pull request #242 from gfldex/master
add Str::trans
2 parents c1510f6 + d793b51 commit 4a4055e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/Type/Str.pod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,28 @@ Example:
681681
682682
"aå«".ords # (97 229 171)
683683
684+
=head2 method trans
685+
686+
Replaces one or many substrings with one or many strings. Ranges are supported, both for keys and values. Regexes work as keys. When called with C<:complemet> anything but the matched value or range is replaced with a single value. With C<:delete> the matched characters are removed. Combining C<:complement> and C<:delete> will remove anything but the mached values. The adverb C<:squash> will reduce repeated matched characters to a single character.
687+
688+
Example:
689+
690+
my $str = 'say $x<b> && $y<a>';
691+
$str.=trans( '<' => '&lt;' );
692+
$str.=trans( '<' => '&lt;', '>' => '&gt;' );
693+
694+
$str.=trans( [ '<' , '>' , '&' ] =>
695+
[ '&lt;', '&gt;', '&amp;' ]);
696+
697+
$str.=trans( ['a'..'y'] => ['A'..'z'] );
698+
699+
"abcdefghij".trans(/<[aeiou]> \w/ => ''); # «cdgh»
700+
701+
"a123b123c".trans(['a'..'z'] => 'x', :complement); # «axxxbxxxc»
702+
"a123b123c".trans('23' => '', :delete); # «a1b1c»
703+
"aaa1123bb123c".trans('a'..'z' => 'A'..'Z', :squash); # «A1123B123C»
704+
"aaa1123bb123c".trans('a'..'z' => 'x', :complement, :squash); # «aaaxbbxc»
705+
684706
=head2 method indent
685707
686708
proto method indent($)

0 commit comments

Comments
 (0)