# Bug in replace! function

**URL:** https://discourse.julialang.org/t/bug-in-replace-function/70388
**Category:** General Usage
**Tags:** strings
**Created:** [October 26, 2021, 8:43am UTC](https://discourse.julialang.org/t/bug-in-replace-function/70388 "2021-10-26T08:43:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Cevheriferd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cevheriferd/32/26792_2.png) [@Cevheriferd](https://discourse.julialang.org/u/Cevheriferd)
#### Post date: [October 26, 2021, 8:43am UTC](https://discourse.julialang.org/t/bug-in-replace-function/70388/1 "2021-10-26T08:43:01Z")

</div>

See the following MWE:

```julia
a = "Hello World" #"Hello World"
b = replace(a,"Hello" => "Bye") #"Bye World"
replace!(a, "Hello" => "Bye") 

#ERROR: LoadError: MethodError: no method matching _replace!(::Base.var"#new#295"{Tuple{Pair{String, String}}}, ::String, ::String, ::Int64)
Closest candidates are:
  _replace!(::Union{Function, Type}, ::AbstractArray, ::AbstractArray, ::Int64) at set.jl:658
  _replace!(::Union{Function, Type}, ::Dict{K, V}, ::AbstractDict, ::Int64) where {K, V} at set.jl:691
  _replace!(::Union{Function, Type}, ::Set{T}, ::AbstractSet, ::Int64) where T at set.jl:723
  ...
Stacktrace:
 [1] replace_pairs!(res::String, A::String, count::Int64, old_new::Tuple{Pair{String, String}})
   @ Base .\set.jl:488
 [2] replace!(A::String, old_new::Pair{String, String}; count::Int64)
   @ Base .\set.jl:478
 [3] replace!(A::String, old_new::Pair{String, String})
   @ Base .\set.jl:478

```

So when I try to change the variable a it gives the error as stated above. It should be possible to adjust variable a with the replace! function right? Thanks for helping me out. I use julia 1.6 btw.

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [October 26, 2021, 8:49am UTC](https://discourse.julialang.org/t/bug-in-replace-function/70388/2 "2021-10-26T08:49:52Z")

</div>

Your problem here is that `replace!` isn’t defined for strings because strings are immutable.

This is also true for other immutable collections. For example, `replace!([1,2,3], 1=>2)` works, but `replace!(1:3, 1=>2)` does not.

---

<div class="post-metadata">

### Author: ![Cevheriferd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cevheriferd/32/26792_2.png) [@Cevheriferd](https://discourse.julialang.org/u/Cevheriferd)
#### Post date: [October 26, 2021, 8:54am UTC](https://discourse.julialang.org/t/bug-in-replace-function/70388/3 "2021-10-26T08:54:03Z")

</div>

ah ok. Thanks for the reply! Is there any particular reason that strings are immutable?

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [October 26, 2021, 9:30am UTC](https://discourse.julialang.org/t/bug-in-replace-function/70388/4 "2021-10-26T09:30:25Z")

</div>

Yes, immutability allows the compiler and runtime to reason about the values of string more easily and execute code more efficiently in time and space.

For space efficiency, immutable strings allow the use of variable-width encodings such as UTF-8 which store text quite compactly. If you had a mutable UTF-8 string, `replace!` (or other per-character mutations) would be approximately as slow as creating a whole new string anyway, because the number of bytes taken by each character isn’t fixed.

I also guess it was an intentional interface choice that the identity of a string _should_ be defined by its value only, in the same way that numbers are.
