String split returns an array with more elements than expected (empty elements)
arrays, javascript, split, string
Solution
This is the correct and expected behavior. Given that you've included the separator in the string, the split function (simplified) takes the part to the left of the separator ("a,b,c,d,e:10") as the first element and the part to the rest of the separator (an empty string) as the second element.
If you're really curious about how split() works, you can check out pages 148 and 149 of the ECMA spec (ECMA 262) at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
Problem
I don't understand this behaviour: ``` var string = 'a,b,c,d,e:10.'; var array = string.split ('.'); ``` I expect this: ``` console.log (array); // ['a,b,c,d,e:10'] console.log (array.length); // 1 ``` but I get this: ``` console.log (array); // ['a,b,c,d,e:10', ''] console.log (array.length); // 2 ``` Why two elements are returned instead of one? How does `split` work? Is there another way to do this?