Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Julia have a string 'contains' substring method?

Tags:

string

julia

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?

like image 773
logankilpatrick Avatar asked Sep 19 '19 12:09

logankilpatrick


Video Answer


1 Answers

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().

like image 200
logankilpatrick Avatar answered Oct 20 '22 14:10

logankilpatrick