Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use `endswith()` against multiple values in Julia?

I have a string and I want to use endswith() on it but against multiple values.

My first guess was to try it with a tuple:

# {string}

suffixes = ({multiple suffixes here})

endswith(i,extensions)

This generated the error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MethodError: no method matching endswith(::String, ::Tuple{String, String})

So, I went looking for official documentation at Julia documentation but they only talk about single string comparions.

I did find this on an unofficial site that says:

If the second argument is a vector or set of characters, tests whether the last character of string belongs to that set.

Which does not quite apply.

I tried the following variations:

endswith(i,extensions[:])

Produces the same error as before
MethodError: no method matching endswith(::String, ::Tuple{String, String})

Variations with list:

# {string}

suffixes = [{multiple suffixes here}]

endswith(i,extensions)

Only changes the error message from tuple to vector

MethodError: no method matching endswith(::String, ::Vector{String})

Or with the index supplied

# {string}

suffixes = [{multiple suffixes here}]

endswith(i,extensions[:])

Same error

MethodError: no method matching endswith(::String, ::Vector{String})

I tried endswith(i,extensions[1:length(extensions)]) with both tuples and vectors and that did not work either.

Anyone familiar?

>Solution :

You probably want the following:

julia> endswith.("abcd", ["d", "c", "cd", "dc"])
4-element BitVector:
 1
 0
 1
 0

Note the . after endswith. This operation broadcasts the function over the collection (in this case vector of suffixes). For more information see here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading