473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

split() quirk

Why does ''.split(',') yield an array with length 1 rather than an
array with length 0?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
5 1151
Christopher Benson-Manica wrote:
Why does ''.split(',') yield an array with length 1 rather than an
array with length 0?


Because the first element of the array will be everything up to
the first ','. You have created an array with one element that
contains nothing. It is equivalent to:

var newArray = [,];

Which has a length of 1, but it contains nothing.
Some examples:

alert( ''.split(',').length) // 1
var z = []; alert(z.length) // 0
var z = [,]; alert(z.length) // 1


--
Fred
Jul 23 '05 #2
Fred Oz <oz****@iinet.net.auau> spoke thus:
Because the first element of the array will be everything up to
the first ','. You have created an array with one element that
contains nothing. It is equivalent to:


I'm sure it's just me, but it seems counterintuitive to me :) Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #3
var z = [,]; alert(z.length) // 1

returns 2 for me, is it that there's a space before and after the comma?

Chris

"Fred Oz" <oz****@iinet.net.auau> wrote in message
news:42***********************@per-qv1-newsreader-01.iinet.net.au...
Christopher Benson-Manica wrote:
Why does ''.split(',') yield an array with length 1 rather than an
array with length 0?


Because the first element of the array will be everything up to
the first ','. You have created an array with one element that
contains nothing. It is equivalent to:

var newArray = [,];

Which has a length of 1, but it contains nothing.
Some examples:

alert( ''.split(',').length) // 1
var z = []; alert(z.length) // 0
var z = [,]; alert(z.length) // 1


--
Fred

Jul 23 '05 #4
JRS: In article <cv**********@chessie.cirr.com>, dated Fri, 25 Feb 2005
14:43:16, seen in news:comp.lang.javascript, Christopher Benson-Manica
<at***@nospam.cyberspace.org> posted :
Why does ''.split(',') yield an array with length 1 rather than an
array with length 0?


I have a vague recollection that the result may be browser-dependent.

But what you (and I) get agrees with ECMA 262, Edn 3, 15.5.4.14, para 3,
IMHO.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines
Jul 23 '05 #5
Chris wrote:
var z = [,]; alert(z.length) // 1

returns 2 for me, is it that there's a space before and after the comma?

No, it's whether you use IE or not. Firefox/Mozilla/Safari all
give 1, but IE gives 2. :-)

--
Fred
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
2
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is...
1
by: Peter Billam | last post by:
Greetings. This problem arises in the context of the CPAN module Crypt::Tea ... It works with IE, Mozilla, Netscape, Firefox, IE and Opera, but if you check out...
5
by: Roland Bengtsson | last post by:
Is it possible to determine if the www-reader is in quirk mode and then do a printout on the status line at the bottom?
23
by: Mark Dickinson | last post by:
I have a simple 192-line Python script that begins with the line: dummy0 = 47 The script runs in less than 2.5 seconds. The variable dummy0 is never referenced again, directly or indirectly,...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.