# Replace two substrings in a string with the same \`replace\` call?

**URL:** https://discourse.julialang.org/t/replace-two-substrings-in-a-string-with-the-same-replace-call/70053
**Category:** General Usage
**Tags:** question, strings
**Created:** [October 19, 2021, 5:17pm UTC](https://discourse.julialang.org/t/replace-two-substrings-in-a-string-with-the-same-replace-call/70053 "2021-10-19T17:17:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [October 19, 2021, 5:17pm UTC](https://discourse.julialang.org/t/replace-two-substrings-in-a-string-with-the-same-replace-call/70053/1 "2021-10-19T17:17:11Z")

</div>

I just found that `replace` could be used to replace two bits of a vector as in

```julia
replace([1, 2, 1, 3], 1=>0, 2=>4)

```

Where the replacements are defined using `Pair`.

I tried that with a string instead of an integer vector, but that seems to fail.

```julia
replace("aabbccdd","a"=>"y","c"=>"z")

```

> ERROR: MethodError: no method matching replace(::String, ::Pair{String, String}, ::Pair{String, String})  
> Closest candidates are:  
> replace(::AbstractString, ::Pair, ::Pair) at set.jl:613  
> replace(::Any, ::Pair…; count) at set.jl:555  
> replace(::Union{Function, Type}, ::Pair, ::Pair; count) at set.jl:612  
> …  
> Stacktrace:  
> [1] replace(a::String, b::Pair{String, String}, c::Pair{String, String})  
> @ Base .\set.jl:613  
> [2] top-level scope  
> @ REPL[93]:1

Is there a specific reason for this to not be implemented / available / authorized ?  
The obvious alternative would be `replace(replace("aabbccdd","a"=>"y"),"c"=>"z")`, but it does not feel quit clean.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [October 19, 2021, 5:27pm UTC](https://discourse.julialang.org/t/replace-two-substrings-in-a-string-with-the-same-replace-call/70053/2 "2021-10-19T17:27:31Z")

</div>

What version of Julia are you using?

```julia
julia> versioninfo()
Julia Version 1.7.0-rc1
Commit 9eade6195e (2021-09-12 06:45 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, ivybridge)
Environment:
  JULIA_REVISE_INCLUDE = 1

julia> replace("aabbccdd","a"=>"y","c"=>"z")
"yybbzzdd"

```

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [October 20, 2021, 7:47am UTC](https://discourse.julialang.org/t/replace-two-substrings-in-a-string-with-the-same-replace-call/70053/3 "2021-10-20T07:47:00Z")

</div>

Yep, I should have said that from the beginning, my bad.

```julia
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, tigerlake)

```

I guess this has been corrected at some point between 1.6.2 and 1.7.0
