String split
Divides a string into parts based on a pattern
We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
If you have a string that references a URL or directory path, you can split it into a list of strings. Using String.split/2
takes in two arguments. Pass in the string and the “/“ slash as arguments.
iex> demo = "/videos/play/elixir_event/230/"
iex> String.split(demo, "/")
# ["", "videos", "play", "elixir_event", "230", ""]
If the path your testing will contain a consisted path, you can use pattern matching to get the items in that list.
In this example I’m only interested in elixir_event
and 230
.
iex> demo = "/videos/play/elixir_event/230/"
iex> [_,_,_,edition,episode,_] = String.split(demo, "/")
# ["", "videos", "play", "elixir_event", "230", ""]
iex> edition
# "elixir_event"
iex> episode
# "230"