Logo

Programming-Idioms

History of Idiom 63 > diff from v38 to v39

Edit summary for version 39 by daxim:
[Perl] use correct variables, replace all occurrences

Version 38

2019-09-29, 12:43:45

Version 39

2019-09-29, 13:52:22

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Extra Keywords
substring substitute
Extra Keywords
substring substitute
Imports
Imports
use 5.014;
Code
substr($a, 5, 2) = "replacement text";
Code
my $x2 = $x =~ s/\Q$y/$z/gr;
Comments bubble
You can use substr on the left side of the equation. Here, we'll replace the two characters at position 5 with the phrase "replacement text". Yes, the replacement text can be of a different length.
Doc URL
http://p3rl.org/op#s