Check if a String Contains a SubString in Julia

Learn the best ways to check whether a String contains a SubString.
Published

April 9, 2023

In this beginner-friendly tutorial, we’ll demonstrate how to check if a string (String) contains a specific substring (SubString) in Julia.

Checking for Substring in a String

For those familiar with Python, you might instinctively attempt to use in or in Julia to verify if a SubString is present in a String. However, these approaches won’t work.

"World"  "Hello, World!"
LoadError: use occursin(needle, haystack) for string containment
"World" in "Hello, World!"
LoadError: use occursin(needle, haystack) for string containment

In Julia, you need to use either occursin or contains:

@test occursin("World", "Hello, World!") == true
Test Passed
@test occursin("world", "Hello, World!") == false
Test Passed
@test contains("Hello, World!", "World") == true
Test Passed
@test contains("Hello, World!", "world") == false
Test Passed

Remember the order of the arguments in contains(haystack, needle) as haystack contains needle. For occursin(needle, haystack), recall the order as needle occursin haystack.

This naming pattern is fairly common in Julia.

Using Regular Expressions (Regex)

In Julia, you can create a regular expression (Regex) by using the string literal macro syntax with the letter r, such as r"regex_pattern".

match(r"World", "Hello, World!")
RegexMatch("World")
@test match(r"World", "Hello, World!") isa RegexMatch
Test Passed
@test match(r"world", "Hello, World!") === nothing
Test Passed

If you’re only interested in determining whether a match exists, use the isnothing function to check the return value of match.

@test !isnothing(match(r"World", "Hello, World!"))
Test Passed

More idiomatically, you can use the occursin or contains functions with Regex objects as well:

@test occursin(r"W.*d", "Hello, World!") === true
Test Passed
@test contains("Hello, World!", r"w.*d") === false
Test Passed

The Regex string literal macro supports adding flags at the end of the string.

@test contains("Hello, World!", r"w.*d"i) === true
Test Passed

Finding the index of a Substring in a String

To obtain the index of the first occurrence of a SubString within a String, you can use the findfirst function:

findfirst("World", "Hello, World!")
8:12

To find all the occurrences of a SubString within a String, you can use the findall function:

findall(r"the"i, "The quick brown fox jumps over the lazy dog.")
2-element Vector{UnitRange{Int64}}:
 1:3
 32:34

Conclusions

In this tutorial, we have explored various methods to determine if a string contains a specific substring in Julia.

With these tools in your toolbox, you should be well-equipped to handle string manipulation tasks in Julia, and hopefully with time, you’ll find that Julia’s built-in functions and idiomatic patterns make working with strings both efficient and enjoyable.

Reuse

Citation

For attribution, please cite this work as:
“Check if a String Contains a SubString in Julia,” Apr. 09, 2023. https://juliacheat.codes/how-to/check-if-string-contains-substring.