split() quirk

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    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)cybers pace.org | don't, I need to know. Flames welcome.
  • Fred Oz

    #2
    Re: split() quirk

    Christopher Benson-Manica wrote:[color=blue]
    > Why does ''.split(',') yield an array with length 1 rather than an
    > array with length 0?
    >[/color]

    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(',').l ength) // 1
    var z = []; alert(z.length) // 0
    var z = [,]; alert(z.length) // 1




    --
    Fred

    Comment

    • Christopher Benson-Manica

      #3
      Re: split() quirk

      Fred Oz <ozfred@iinet.n et.auau> spoke thus:
      [color=blue]
      > 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:[/color]

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

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Chris

        #4
        Re: split() quirk

        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" <ozfred@iinet.n et.auau> wrote in message
        news:421f4676$0 $30570$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue]
        > Christopher Benson-Manica wrote:[color=green]
        > > Why does ''.split(',') yield an array with length 1 rather than an
        > > array with length 0?
        > >[/color]
        >
        > 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(',').l ength) // 1
        > var z = []; alert(z.length) // 0
        > var z = [,]; alert(z.length) // 1
        >
        >
        >
        >
        > --
        > Fred[/color]


        Comment

        • Dr John Stockton

          #5
          Re: split() quirk

          JRS: In article <cvndi4$2ce$1@c hessie.cirr.com >, dated Fri, 25 Feb 2005
          14:43:16, seen in news:comp.lang. javascript, Christopher Benson-Manica
          <ataru@nospam.c yberspace.org> posted :
          [color=blue]
          >Why does ''.split(',') yield an array with length 1 rather than an
          >array with length 0?[/color]

          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.demo n.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
          <URL:http://www.bancoems.co m/CompLangPascalD elphiMisc-MiniFAQ.htm> clpdmFAQ;
          <URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines

          Comment

          • Fred Oz

            #6
            Re: split() quirk

            Chris wrote:[color=blue]
            > var z = [,]; alert(z.length) // 1
            >
            > returns 2 for me, is it that there's a space before and after the comma?[/color]


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

            --
            Fred

            Comment

            Working...