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 10:51, 4 February 2024
, 4 months ago→{{header|Wren}}: Changed to Wren S/H
(→{{header|Ruby}}: Add Ruby) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(One intermediate revision by one other user not shown) | |||
Line 21:
{{Template:Strings}}
<br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V rep = [‘a’ = [1 = ‘A’, 2 = ‘B’, 4 = ‘C’, 5 = ‘D’], ‘b’ = [1 = ‘E’], ‘r’ = [2 = ‘F’]]
F trstring(oldstring, repdict)
DefaultDict[Char, Int] seen
V newchars = ‘’
L(c) oldstring
V i = ++seen[c]
newchars ‘’= I c C repdict & i C repdict[c] {repdict[c][i]} E c
R newchars
print(‘abracadabra -> ’trstring(‘abracadabra’, rep))</syntaxhighlight>
{{out}}
<pre>
abracadabra -> AErBcadCbFD
</pre>
=={{header|ALGOL 68}}==
Line 1,148 ⟶ 1,168:
{{libheader|Wren-regex}}
Not particularly succinct but, thanks to a recently added library method, better than it would have been :)
<syntaxhighlight lang="
import "./str" for Str
Line 1,167 ⟶ 1,187:
Alternatively, using regular expressions (embedded script) producing output as before.
<syntaxhighlight lang="
var s = "abracadabra"
|