Selectively replace multiple instances of a character within a string: Difference between revisions
Selectively replace multiple instances of a character within a string (view source)
Revision as of 13:39, 31 May 2022
, 2 years agoAdded XPL0 example.
Thundergnat (talk | contribs) m (→{{header|Raku}}: DRY) |
(Added XPL0 example.) |
||
Line 75:
s = Regex.compile("r").replaceAll(s, "F", 2, 1)
System.print(s)</lang>
=={{header|XPL0}}==
<lang XPL0>string 0;
proc Mangle(S);
char S, A, B, R;
[A:= "ABaCD"; B:= "Eb"; R:= "rF";
while S(0) do
[case S(0) of
^a: [S(0):= A(0); A:= A+1];
^b: [S(0):= B(0); B:= B+1];
^r: [S(0):= R(0); R:= R+1]
other [];
S:= S+1;
];
];
char S;
[S:= "abracadabra";
Text(0, S); Text(0, " -> "); Mangle(S); Text(0, S); CrLf(0);
S:= "caarabadrab";
Text(0, S); Text(0, " -> "); Mangle(S); Text(0, S); CrLf(0);
]</lang>
{{out}}
<pre>
abracadabra -> AErBcadCbFD
caarabadrab -> cABraECdFDb
</pre>
|