Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.split() in elixir placing a empty string at the end of the list of substrings in the output

Tags:

split

elixir

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?

like image 393
shubhamagiwal92 Avatar asked Aug 23 '17 22:08

shubhamagiwal92


1 Answers

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"]
like image 200
achedeuzot Avatar answered Oct 21 '22 00:10

achedeuzot