HighDots Forums  

Behaviour of string.split()

Javascript JavaScript language (comp.lang.javascript)


Discuss Behaviour of string.split() in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Steve
 
Posts: n/a

Default Behaviour of string.split() - 08-13-2008 , 06:59 AM






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.

Reply With Quote
  #2  
Old   
Henry
 
Posts: n/a

Default Re: Behaviour of string.split() - 08-13-2008 , 07:24 AM






On Aug 13, 11:59 am, Steve wrote:
Quote:
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.prototype.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'.


Reply With Quote
  #3  
Old   
virtuPIC
 
Posts: n/a

Default Re: Behaviour of string.split() - 08-13-2008 , 07:26 AM



Quote:
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


Reply With Quote
  #4  
Old   
Steve
 
Posts: n/a

Default Re: Behaviour of string.split() - 08-13-2008 , 07:34 AM



On Aug 13, 1:26 pm, virtuPIC <WebMas... (AT) airspace-v (DOT) com> wrote:
Quote:
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.



Reply With Quote
  #5  
Old   
Peter Billam
 
Posts: n/a

Default Re: Behaviour of string.split() - 08-13-2008 , 09:30 PM



On 2008-08-13, Gregor Kofler <usenet (AT) gregorkofler (DOT) at> wrote:
Quote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.