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 14:26, 31 May 2022
, 2 years agoAdded Go
(Added XPL0 example.) |
(Added Go) |
||
Line 21:
{{Template:Strings}}
<br>
=={{header|Go}}==
{{trans|Wren}}
<lang go>package main
import (
"fmt"
"strings"
)
func main() {
s := "abracadabra"
ss := []byte(s)
var ixs []int
for ix, c := range s {
if c == 'a' {
ixs = append(ixs, ix)
}
}
repl := "ABaCD"
for i := 0; i < 5; i++ {
ss[ixs[i]] = repl[i]
}
s = string(ss)
s = strings.Replace(s, "b", "E", 1)
s = strings.Replace(s, "r", "F", 2)
s = strings.Replace(s, "F", "r", 1)
fmt.Println(s)
}</lang>
{{out}}
<pre>
AErBcadCbFD
</pre>
=={{header|Raku}}==
|