-5

I have two scenario in both the case split() behaves differently

1.When we say

var x ="dadasd\n\n\nsdfsfsdf"
var y=x.split('\n')

then value of is y
["dadasd", "", "", "sdfsfsdf"]

but i was expecting y to be

["dadasd", "", "","","sdfsfsdf"]

2.now when we say

var z ="fsdfsfs\n\n\n"
undefined
var a =z.split("\n")
undefined
a
["fsdfsfs", "", "", ""]

This time I am getting expected value why these two scenario has different behavior

Why does split() behaves differently in first case and do we have any function in java script which can give me expected result in my first scenario

Vicky Kumar
  • 1,358
  • 1
  • 14
  • 26
  • 1
    FWIW I don't think that's a good duplicate. This could have been the same question with any separator char, just that it happened to be newline. – James Thorpe Dec 01 '16 at 16:56
  • @JamesThorpe Okay, re-opened. – Praveen Kumar Purushothaman Dec 01 '16 at 16:58
  • @JamesThorpe - I agree that that's a bad duplicate. On the other hand, it's hard to imagine that this hasn't been asked and answered before. – Ted Hopp Dec 01 '16 at 16:58
  • @TedHopp Yeah - I'm trying to find a better one. [This](http://stackoverflow.com/questions/12836062/string-split-returns-an-array-with-two-elements-instead-of-one) is the closest so far, but not ideal. – James Thorpe Dec 01 '16 at 16:59

1 Answers1

5

The behaviors are consistent and correct. The \n separates tokens. In the first case, with three \n, there are two empty tokens ("") between the three \n and one non-empty token on either side. In the second case, there are two empty tokens between the \n, a non-empty token before the sequence, and another empty token after the third \n. Hence the output.

Another way to think about it is: the \n delimiters don't correspond to output strings. Instead, for each \n, there's a token on the left (which may be empty, if to the left there's another \n or the start of the string) and a token on the right (which, again, may be empty). (Of course, a token that's to the left of one delimiter and to the right of another only appears in the output once.) Think of each occurrence of a delimiter, and also the start and end of the entire string, as fence posts. The output of split() is then an array of what's in between the fence posts. So if you have three delimiters, you have five fence posts, and therefore four output tokens.

It might be instructive to read about Fencepost errors.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521