# Method for counting characters in string? count("a", "batman") vs. count('a', "batman")

**URL:** <https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [December 2, 2020, 10:58pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153 "2020-12-02T22:58:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradeneliason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradeneliason/32/29031_2.png) [@bradeneliason](https://discourse.julialang.org/u/bradeneliason)\
**Post date:** [December 2, 2020, 10:58pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/1 "2020-12-02T22:58:52Z")

</div>

It seems like there should be a valid method for a calling count with a character and a string.

```julia
count("a", "batman") # returns 2 as expected
count('a', "batman") # ERROR: MethodError: objects of type Char are not callable
count(==('a'), "batman") # returns 2, an annoying workaround

```

Is there a reason why this count method isn’t supported. Thoughts?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 2, 2020, 11:21pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/2 "2020-12-02T23:21:32Z")

</div>

The function that should do the job is this one (from base/regex.jl):

```julia
 function count(t::Union{AbstractString,Regex}, s::AbstractString; overlap::Bool=false)
           n = 0
           i, e = firstindex(s), lastindex(s)
           while true
               r = findnext(t, s, i)
               isnothing(r) && break
               n += 1
               j = overlap || isempty(r) ? first(r) : last(r)
               j > e && break
               @inbounds i = nextind(s, j)
           end
           return n
       end

```

If one adds “AbstractChar” to the union, that function would be called and results what is expected:

```julia
julia> function mycount(t::Union{AbstractString,Regex,AbstractChar}, s::AbstractString; overlap::Bool=false)
           n = 0
           i, e = firstindex(s), lastindex(s)
           while true
               r = findnext(t, s, i)
               isnothing(r) && break
               n += 1
               j = overlap || isempty(r) ? first(r) : last(r)
               j > e && break
               @inbounds i = nextind(s, j)
           end
           return n
       end
mycount (generic function with 2 methods)

julia> mycount("a","batman")
2

julia> mycount('a',"batman")
2

```

I am not sure if there is any bad implication on doing that, but it seems a simple fix.

---

<div class="post-metadata">

**Author:** ![bradeneliason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradeneliason/32/29031_2.png) [@bradeneliason](https://discourse.julialang.org/u/bradeneliason)\
**Post date:** [December 2, 2020, 11:25pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/3 "2020-12-02T23:25:37Z")

</div>

Thanks Leandro. Indeed it seems like a quick fix. I tested pretty much that exact code after posting.

It seems like such an obvious thing that I figured there would have been some discussion about and possibly a reason for not implementing it.

---

<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:** [December 2, 2020, 11:37pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/4 "2020-12-02T23:37:09Z")

</div>

Please feel free to make a PR with this addition and a testcase.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 3, 2020, 12:01am UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/5 "2020-12-03T00:01:57Z")

</div>

I’ll do it carefully asap.

Edit: done ([https://github.com/JuliaLang/julia/pull/38675](https://github.com/JuliaLang/julia/pull/38675)

I have not much experience with this, but I hope that is fine.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 3, 2020, 2:25pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/6 "2020-12-03T14:25:08Z")

</div>

There is a similar possible issue with `findall`:

```julia
julia> findall("a","batman")
2-element Array{UnitRange{Int64},1}:
 2:2
 5:5

julia> findall('a',"batman")
ERROR: MethodError: no method matching findall(::Char, ::String)

julia> findall(==('a'),"batman")
2-element Array{Int64,1}:
 2
 5

```

Should we add a new method to findall? The error message is not as bad as that of `count` , but it seems reasonable to expect `findall` to find all chars and return their positions.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 3, 2020, 3:27pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/7 "2020-12-03T15:27:50Z")

</div>

Yes, it is an omission as `findfirst` and `findlast` already work with a Char as the first argument.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 3, 2020, 4:57pm UTC](https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153/8 "2020-12-03T16:57:45Z")

</div>

I have added a method there for `findall` as well.
