I am new to Elixir. I am trying a basic operation to split the string which is as follows
String.split("Awesome","");
According to the elixir document, it should split the string based on the pattern provided. The output
I get is as follows
["A", "w", "e", "s", "o", "m", "e", ""]
I was expecting an output
as follows
["A", "w", "e", "s", "o", "m", "e"]
My Elixir
and Elang
version are as follows
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Elixir 1.5.1
I am unable to understand why an empty string
is added at the end
of the list of substrings
in the output. Can somebody let me know why I am getting the above output and help with the expected output?
I guess it's the expected behaviour. The link you provided also says the following thing:
Empty strings are only removed from the result if the
:trim
option is set to true.
It's in one of the examples with the solution:
Splitting on empty patterns returns graphemes:
iex> String.split("abc", "")
["a", "b", "c", ""]
iex> String.split("abc", "", trim: true)
["a", "b", "c"]
If you're wondering what "graphemes" are, the Elixir documentation has this explained too at the beginning of the String documentation.
Finally, there are two other useful functions if you want to dig into graphemes: String.codepoints/1
and String.graphemes/1
. For example:
iex> noel = "noe\u0308l"
"noël"
iex> String.codepoints(noel)
["n", "o", "e", "̈", "l"]
iex> String.graphemes(noel)
["n", "o", "ë", "l"]
# Let's try with your example
iex> String.codepoints("Awesome")
["A", "w", "e", "s", "o", "m", "e"]
iex> String.graphemes("Awesome")
["A", "w", "e", "s", "o", "m", "e"]
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