# Contains vs occursin

**URL:** <https://discourse.julialang.org/t/contains-vs-occursin/133737>\
**Category:** New to Julia\
**Created:** [November 7, 2025, 4:42pm UTC](https://discourse.julialang.org/t/contains-vs-occursin/133737 "2025-11-07T16:42:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Queenvictoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/queenvictoria/32/218740_2.png) [@Queenvictoria](https://discourse.julialang.org/u/Queenvictoria)\
**Post date:** [November 7, 2025, 4:42pm UTC](https://discourse.julialang.org/t/contains-vs-occursin/133737/1 "2025-11-07T16:42:58Z")

</div>

Is there any functional difference between contains and occursin? Is it up to preference?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 7, 2025, 4:47pm UTC](https://discourse.julialang.org/t/contains-vs-occursin/133737/2 "2025-11-07T16:47:55Z")

</div>

It looks like `occursin` additionally supports IOBuffer haystacks in addition to `AbstractString` (as well as an undocumented `offset` kwarg with regexes), but otherwise the two are identical. In fact, `contains` is defined by occursin:

```julia-auto
julia> methods(occursin)
# 5 methods for generic function "occursin" from Base:
 [1] occursin(r::Regex, s::SubString{String}; offset)
     @ regex.jl:311
 [2] occursin(haystack)
     @ strings/search.jl:885
 [3] occursin(r::Regex, s::AbstractString; offset)
     @ regex.jl:306
 [4] occursin(needle::Union{AbstractChar, AbstractString}, haystack::AbstractString)
     @ strings/search.jl:860
 [5] occursin(delim::UInt8, buf::Base.GenericIOBuffer)
     @ iobuffer.jl:977

julia> methods(contains)
# 2 methods for generic function "contains" from Base:
 [1] contains(needle)
     @ strings/util.jl:197
 [2] contains(haystack::AbstractString, needle)
     @ strings/util.jl:140

julia> less(contains, (AbstractString,Any))
contains(haystack::AbstractString, needle) = occursin(needle, haystack)

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [November 7, 2025, 8:22pm UTC](https://discourse.julialang.org/t/contains-vs-occursin/133737/3 "2025-11-07T20:22:39Z")

</div>

A difference related to the different order of the arguments is that `contains(x)` can be used to quickly filter an iterable, for example

```julia-auto
julia> filter(contains("hello"), ["hello world", "foo bar"])
1-element Vector{String}:
 "hello world"

```

but

```julia-auto
julia> filter(occursin("hello"), ["hello world", "foo bar"])
String[]

```

It’s not that they’re functionally different, but the order of the arguments matter for the one-argument methods.
