Search code examples
elixirurl-parsing

How to extract the filename from a URL in Elixir?


Here's my problem I want to get the file name from the URL ex. https://randomWebsite.com/folder/filename.jpeg I got the expected result on javascript using this string.substring(string.lastIndexOf('/')+1). In Elixir I use this function String.slice(string, <first_value_from_binary.match>..String.length(string) ... :binary.match() only gets the first index of the first char that match the given letter... or is there any other solution getting the file name from the URL than this?


Solution

  • I think only need Path.basename/1:

    "https://randomWebsite.com/folder/filename.jpeg"
    |> Path.basename()