Behaviour of string.split()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Behaviour of string.split()

    I understand that string.split() should produce an array from a
    string. However when I use the following script the type of the result
    is indeed an object but the array elements are undefined. Why?

    var strTest = '1,2,3,4';
    var aryTest = strTest.split() ;
    alert(typeof aryTest + " " + aryTest[2]);

    Thanks in advance.
  • Henry

    #2
    Re: Behaviour of string.split()

    On Aug 13, 11:59 am, Steve wrote:
    I understand that string.split() should produce an
    array from a string. However when I use the
    following script the type of the result is indeed
    an object but the array elements are undefined. Why?
    >
    var strTest = '1,2,3,4';
    var aryTest = strTest.split() ;
    alert(typeof aryTest + " " + aryTest[2]);
    If you do not provide the first ("Separator" ) argument to -
    String.prototyp e.split - it will return a one element array with first
    (zero index) element containing a string that is equivalent to the
    original string. Thus - arryTest[2] - would be expected to result in
    the undefined value as the - length - of - arrayTest - will be one and
    its only existing 'array index' property is '0'.

    Comment

    • virtuPIC

      #3
      Re: Behaviour of string.split()

      var strTest = '1,2,3,4';
      var aryTest = strTest.split() ;
      alert(typeof aryTest + "  " + aryTest[2]);
      Hey, you didn't tell your JavaScript VM where to split! Try this:

      var strTest = '1,2,3,4';
      var aryTest = strTest.split(' ,');
      alert(typeof aryTest + " " + aryTest[2]);

      Looks better?

      virtuPIC

      --
      Airspace V - international hangar flying!
      http://www.airspace-v.com/ggadgets for tools & toys

      Comment

      • Steve

        #4
        Re: Behaviour of string.split()

        On Aug 13, 1:26 pm, virtuPIC <WebMas...@airs pace-v.comwrote:
        var strTest = '1,2,3,4';
        var aryTest = strTest.split() ;
        alert(typeof aryTest + " " + aryTest[2]);
        >
        Hey, you didn't tell your JavaScript VM where to split! Try this:
        >
        var strTest = '1,2,3,4';
        var aryTest = strTest.split(' ,');
        alert(typeof aryTest + " " + aryTest[2]);
        >
        Looks better?
        >
        Yes it does.

        However according to the material I have read, including the Rhino,
        the comma is the default. But I should have thought of that anyway.

        Many Thanks,

        Steve.

        Comment

        • Gregor Kofler

          #5
          Re: Behaviour of string.split()

          Steve meinte:
          On Aug 13, 1:26 pm, virtuPIC <WebMas...@airs pace-v.comwrote:
          >>var strTest = '1,2,3,4';
          >>var aryTest = strTest.split() ;
          >>alert(typeo f aryTest + " " + aryTest[2]);
          >Hey, you didn't tell your JavaScript VM where to split! Try this:
          >>
          >var strTest = '1,2,3,4';
          >var aryTest = strTest.split(' ,');
          >alert(typeof aryTest + " " + aryTest[2]);
          >>
          >Looks better?
          >>
          Yes it does.
          >
          However according to the material I have read, including the Rhino,
          the comma is the default. But I should have thought of that anyway.
          Read again. Mozilla doesn't state a "default separator". Instead
          "If separator is omitted, the array returned contains one element
          consisting of the entire string." [1]
          I suppose other ECMAScript derivates behave equally.

          Gregor


          [1]
          <http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Reference: Objects:String: split>

          --
          http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
          http://web.gregorkofler.com ::: meine JS-Spielwiese
          http://www.image2d.com ::: Bildagentur für den alpinen Raum

          Comment

          • Peter Billam

            #6
            Re: Behaviour of string.split()

            On 2008-08-13, Gregor Kofler <usenet@gregork ofler.atwrote:
            Steve meinte:
            However according to the material I have read, including the Rhino,
            the comma is the default. But I should have thought of that anyway.
            >
            Read again. Mozilla doesn't state a "default separator". Instead
            "If separator is omitted, the array returned contains one element
            consisting of the entire string." [1]
            I suppose other ECMAScript derivates behave equally.
            Probably the OP is thinking of the inverse operation, Array.join()
            where Flanagan's O'Reilly book says "if the argument is omitted,
            a comma is used."

            Regards, Peter Billam

            --
            Peter Billam www.pjb.com.au www.pjb.com.au/comp/contact.html

            Comment

            Working...