I am trying to test whether or not a string contains a specific substring and then if it does, the index at which it occurs. How can I do that in Julia?
Julia has a couple of helpful functions that will let you do what a traditional 'contains' function would do.
occursin()
takes two arguments. The first of which is the substring you are looking for and the second being the target or string you will be searching in.
findfirst()
takes the same arguments but this time returns the index of the first occurrence of the substring if it's found.
julia> a = "HelloWorld"
"HelloWorld"
julia> occursin("Hello", a)
true
julia> findfirst("Hello", a)
1:5
See the Julia docs here for more reading on findfirst()
or here for occursin()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With