# Why no mutating filter function for strings?

**URL:** <https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 16, 2020, 6:11am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213 "2020-11-16T06:11:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 16, 2020, 6:11am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/1 "2020-11-16T06:11:59Z")

</div>

Is there a reason why

```julia
julia> str = " a"
" a"

julia> filter(isspace, str)
" "

```

works but

```julia
julia> filter!(isspace, str)
ERROR: MethodError: no method matching filter!(::typeof(isspace), ::String)
Closest candidates are:
  filter!(::Any, ::AbstractArray{T,1} where T) at array.jl:2503
  filter!(::Any, ::IdDict) at iddict.jl:162
  filter!(::Any, ::Dict{K,V}) where {K, V} at dict.jl:686
  ...
Stacktrace:
 [1] top-level scope at REPL[3]:1

```

doesn’t? Why there isn’t a mutating form of `filter` for strings?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 16, 2020, 6:16am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/2 "2020-11-16T06:16:02Z")

</div>

This is probably because strings are immutable. Methods with `!` work by modifying the container passed to them, but you can’t modify a string in Julia (for performance reasons).

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 16, 2020, 6:17am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/3 "2020-11-16T06:17:56Z")

</div>

Putting 2 and 2 together, I realise that other functions return `SubString`s.

Though I just end up using `str = filter(..., str)` anyway, changing the original variable.

Though I’m surprised that there’s no equivalent of `push!` and `deleteat!`. I’ll read up more on this.

Thanks!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 16, 2020, 6:52am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/4 "2020-11-16T06:52:42Z")

</div>

You could always make an issue asking for this feature. That said, one critical difference between a function like `filter` and a function like `findfirst` is that the latter always returns a result which can be described by a single start and end index, while the former can require extensive auxiliary space to store the result (e.g. `filter(x->x=='0', "0110010101010")` )

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2020, 8:38am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/5 "2020-11-16T08:38:45Z")

</div>

> [@kapple](#):
>
> Though I’m surprised that there’s no equivalent of `push!` and `deleteat!`

What would they do on an immutable object?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 16, 2020, 8:56am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/6 "2020-11-16T08:56:39Z")

</div>

> [@Oscar\_Smith](#):
>
> You could always make an issue asking for this feature.

Sorry, I’m coming across wrong. I’m new to Julia and am learning why the design of the language is in certain ways. My asking was not an expectation of conformation to a structure, but an inquiry for better understanding, challenging my intuition. I couldn’t find anything in the documentation explaining, so I thought I’d ask.

> [@Tamas\_Papp](#):
>
> What would they do on an immutable object?

Throw an error I suppose.

Again, not expecting a change from my question. Just looking for understanding, and strings being immutable for performance makes sense. Thanks!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2020, 8:59am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/7 "2020-11-16T08:59:23Z")

</div>

> [@kapple](#):
>
> Throw an error I suppose.

But they already do, eg

```julia
julia> push!("I am immutable", 'q')
ERROR: MethodError: no method matching push!(::String, ::Char)

```

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 16, 2020, 9:02am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/8 "2020-11-16T09:02:37Z")

</div>

And likewise for `filter!` as per my original post, yes.

I was thinking an error that mentions an inappropriate attempt to mutate an immutable object, rather than no method defined for that combination of input variable types.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 22, 2024, 2:46am UTC](https://discourse.julialang.org/t/why-no-mutating-filter-function-for-strings/50213/9 "2024-07-22T02:46:55Z")

</div>

A post was split to a new topic: [Why are Julia strings immutable?](https://discourse.julialang.org/t/why-are-julia-strings-immutable/117320)
