Check if a String Contains a Substring in Julia

Check if a String Contains a Substring in Julia

  • Julia
  • 3 mins read

To check if a string contains a substring in Julia, you can use the occursin function. This function returns true if the substring is found in the string, and false otherwise.

Check if a String Contains a Substring in Julia Examples

Here is an example of using the occursin function to check if a string contains a substring:

julia> s = "The quick brown fox"

julia> occursin("fox", s)
true

julia> occursin("dog", s)
false

In the above code, the occursin function is used to check if the string "fox" is contained in the string s. Since "fox" is present in s, the occursin function returns true.

Another example is using the occursin function in a conditional statement:

julia> if occursin("fox", s)
           println("'fox' was found in the string")
       else
           println("'fox' was not found in the string")
       end
'fox' was found in the string

In the above code, the if statement uses the occursin function to check if the string "fox" is contained in s. Since "fox" is found in s, the if statement executes the code in the first block, printing a message indicating that "fox" was found in the string.

You can also use the in operator to check if a string contains a substring in Julia. This is equivalent to using the occursin function, but it has a different syntax. Here is an example:

julia> if "fox" in s
           println("'fox' was found in the string")
       else
           println("'fox' was not found in the string")
       end
'fox' was found in the string

In the above code, the in operator is used to check if the string "fox" is contained in s. Since "fox" is found in s, the if statement executes the code in the first block, printing a message indicating that "fox" was found in the string.

Here is an advanced example that demonstrates some additional features of the occursin function:

julia> s = "The quick brown fox"
"The quick brown fox"

julia> # Check if the substring occurs at the beginning of the string
julia> occursin("The", s)
true

julia> # Check if the substring occurs at the end of the string
julia> occursin("fox", s, at_end=true)
true

julia> # Check if the substring occurs after a specific position in the string
julia> occursin("brown", s, at_start=7)
true

julia> # Check if the substring occurs multiple times in the string
julia> s = "The fox is quick and the fox is brown"
"The fox is quick and the fox is brown"
julia> occursin("fox", s)
true
julia> findall(occursin("fox"), s)
2-element Array{Int64,1}:
  5
  24

In this example, we first check if the substring occurs at the beginning of the string using the at_start option. Then we check if the substring occurs at the end of the string using the at_end option. Next, we check if the substring occurs after a specific position in the string using the at_start option. Finally, we use the findall function to find all occurrences of the substring in the string. For more information, I would recommend checking the documentation for occursin.

Related:

  1. Check if a Variable Exists in Julia
  2. Check if nothing in Juila