# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yan](https://avatars.discourse-cdn.com/v4/letter/y/3e96dc/32.png) [@Yan](https://discourse.julialang.org/u/Yan)\
**Post date:** [November 6, 2019, 5:35am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/1 "2019-11-06T05:35:08Z")

</div>

How to realize the following perl translating task in Julia?

```
#/user/bin/perl

```

my $string = ‘The cat sat on the mat’;  
$string =~ tr/ao/oL/;  
print “$string\n”;

#-------------------------  
The cot sot Ln the mot

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 6, 2019, 5:36am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/2 "2019-11-06T05:36:40Z")

</div>

please use docs

[https://docs.julialang.org/en/v1/base/strings/#Base.replace-Tuple{AbstractString,Pair}](https://docs.julialang.org/en/v1/base/strings/#Base.replace-Tuple%7BAbstractString,Pair%7D)

---

<div class="post-metadata">

**Author:** ![Yan](https://avatars.discourse-cdn.com/v4/letter/y/3e96dc/32.png) [@Yan](https://discourse.julialang.org/u/Yan)\
**Post date:** [November 7, 2019, 1:32am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/3 "2019-11-07T01:32:25Z")

</div>

which one ?

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 7, 2019, 1:44am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/4 "2019-11-07T01:44:52Z")

</div>

```julia
function tr(s::String,target::String,sub::String)
    return Base.replace(s,target => sub)
end

mystring = "The cat sat on the mat"
mystring = tr(mystring,"at","ot")
println(mystring)

```

```julia
$ julia zzz.jl
The cot sot on the mot

```

---

<div class="post-metadata">

**Author:** ![Yan](https://avatars.discourse-cdn.com/v4/letter/y/3e96dc/32.png) [@Yan](https://discourse.julialang.org/u/Yan)\
**Post date:** [November 7, 2019, 2:37am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/5 "2019-11-07T02:37:29Z")

</div>

It doesn’t work properly. The request ![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f18ef7f677f7074ee93854f7128eadf10a92cdde.png) means every character changes ( ‘a’ to ‘o’ and ‘o’ to ‘L’) independently against the original string.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 7, 2019, 3:56am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/6 "2019-11-07T03:56:14Z")

</div>

```julia
julia> function g(s, tar, rep)
           @assert length(tar) == length(rep)
           replace(collect(s), Dict(zip(tar,rep))...) |> join
       end
g (generic function with 1 method)

julia> g("The cat sat on the mat", "ao", "oL")
"The cot sot Ln the mot"

```

---

<div class="post-metadata">

**Author:** ![Yan](https://avatars.discourse-cdn.com/v4/letter/y/3e96dc/32.png) [@Yan](https://discourse.julialang.org/u/Yan)\
**Post date:** [November 7, 2019, 4:26am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/7 "2019-11-07T04:26:37Z")

</div>

Quite smart solution. 👍

but it costs 0.335752 seconds for 100000 loops. In contrast, Perl costs only 0.015 seconds.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 7, 2019, 4:30am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/8 "2019-11-07T04:30:42Z")

</div>

You never told us that performance is a requirement.

---

<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, 4:35am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/9 "2019-11-07T04:35:19Z")

</div>

Also, how did you get those timings? Make sure that you don’t benchmark in global scope.

---

<div class="post-metadata">

**Author:** ![Yan](https://avatars.discourse-cdn.com/v4/letter/y/3e96dc/32.png) [@Yan](https://discourse.julialang.org/u/Yan)\
**Post date:** [November 7, 2019, 4:36am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/10 "2019-11-07T04:36:39Z")

</div>

@time begin  
end

in global scope 0.335752 seconds  
only for loops (excluding definition of function) 0.199178 seconds

---

<div class="post-metadata">

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

</div>

> [@jling](#):
>
> function g(s, tar, rep) @assert length(tar) == length(rep) replace(collect(s), Dict(zip(tar,rep))…) |\> join end

try this

```julia
# Implement Perl tr function
function perl_tr(s::AbstractString, target::AbstractString, rep::AbstractString)
    @assert length(target) == length(rep)
    Base.replace(collect(s), Dict(zip(target,rep))...) |> join
end

let
    mystring = "The cat sat on the mat"
    mystring = perl_tr(mystring,"ao","oL")
    println(mystring)
end

```

typecasting it to AbstractString could make it faster.

---

<div class="post-metadata">

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

</div>

so, don’t to that!

---

<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:06am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/13 "2019-11-07T05:06:49Z")

</div>

This doesn’t type cast anything. Generally, explicit types in function signatures don’t matter for performance.

---

<div class="post-metadata">

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

</div>

pretty sure the cost is in `zip` and `dict`, maybe just do a for loop.

---

<div class="post-metadata">

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

</div>

> This doesn’t type cast anything. Generally, explicit types in function signatures don’t matter for performance.

Doesn’t the compiler only generate code for AbstractString and for nothing else? So less code equals better performance.

---

<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:11am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/16 "2019-11-07T05:11:15Z")

</div>

No. It always specializes on the actual runtime types. Independent of the function signature. Explicit types only have the effect of a filter: don’t except anything but only subtypes of `AbstractStrings`.

---

<div class="post-metadata">

**Author:** ![Yan](https://avatars.discourse-cdn.com/v4/letter/y/3e96dc/32.png) [@Yan](https://discourse.julialang.org/u/Yan)\
**Post date:** [November 7, 2019, 5:12am UTC](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778/17 "2019-11-07T05:12:33Z")

</div>

To be convenient for test, the Perl script attached here.

#!/usr/bin/perl  
use strict;  
use warnings;

my $i=100000;  
my $start=times;  
while ($i\>1) {  
my $string = ‘The cat sat on the mat’;  
$string =~ tr/ao/oL/;  
print “$string\n”;  
$i–;  
}  
print times-$start," seconds\n";

---

<div class="post-metadata">

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

</div>

> [@Yan](#):
>
> print “$string\n”;

You should comment out print “$string\n”; unless you want IO to be part of your benchmark.

---

<div class="post-metadata">

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

</div>

can you please wrap your code with ``` for readability?

---

<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.

[Next page](https://discourse.julialang.org/t/how-to-replace-the-charactors-in-the-string/30778.md?page=2)
