# How to replace the charactors in the string?

**URL:** <https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 6, 2019, 5:35am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778 "2019-11-06T05:35:08Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 7, 2019, 5:31am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/20 "2019-11-07T05:31:58Z")

</div>

I wonder why there is no multi-pair version of `replace` for strings. Anyways, the straightforward

```julia
function h(s)
        snew = replace(s, 'o'=>'L')
        return replace(snew, 'a'=>'o')
end

```

is already ~3x faster than the other Julia implementation in this thread:

```julia
using BenchmarkTools

function g(s, tar, rep)
           @assert length(tar) == length(rep)
           replace(collect(s), Dict(zip(tar,rep))...) |> join
end

function h(s)
        snew = replace(s, 'o'=>'L')
        return replace(snew, 'a'=>'o')
end

@btime g("The cat sat on the mat", "ao", "oL"); # 1.660 μs
@btime h("The cat sat on the mat"); # 527.368 ns

```

Running your perl script (after removing the print statements), I get: `15-31 ms` which is much slower. I don’t know if the perl benchmark is any good (I don’t know how to properly benchmark in perl). But taking it for what it is, the Julia implementation `h` is more than 2500x faster.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778)._
