473,698 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Aug 13 '08 #1
5 1554
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'.
Aug 13 '08 #2
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
Aug 13 '08 #3
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.

Aug 13 '08 #4
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
Aug 13 '08 #5
On 2008-08-13, Gregor Kofler <us****@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
Aug 14 '08 #6

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

Similar topics

5
31176
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 will split myString up using the delimiter of 1 space so that
9
4346
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. I would have expected the second example to have also returned an empty
4
1479
by: bill | last post by:
Consider: >>> import shlex >>> shlex.split('$(which sh)') Is this behavior correct? It seems that I should either get one token, or the list , but certainly breaking it the way it does is erroneous.
4
1533
by: Tim Streater | last post by:
I have this: splitter = //; dateItems = dateString.split (splitter, 3); where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5. But it might also be nullstring or any junk the user types in. Now I find that with the code above, and a null string, .split gives up and I get a JavaScript error, instead of what I might expect which would be
2
1660
by: Adam Honek | last post by:
I have the following code below. The thing is, even if txtCCEmailAddress.Text is empty (no text), CCRecipeant will return a Ubound() higher than 0 and still go in the loop. If there's nothing to split how can CCRecipeant be a positive UBound value? 'Get the number of specified CC recipeants CCRecipeant = txtCCEmailAddress.Text.Split(";")
9
1790
by: horizon5 | last post by:
Hi, my collegues and I recently held a coding style review. All of the code we produced is used in house on a commerical project. One of the minor issues I raised was the common idiom of specifing: <pre> if len(x) 0: do_something() </pre>
4
1512
by: schnupfy | last post by:
Hi, I am not used to python and I am wondering about this thing: If I execute this from the shell: /root/mk/services.py 192.168.1.101 critical "192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2- MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2- SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
3
1564
by: Walter Cruz | last post by:
Hi all! Just a simple question about the behaviour of a regex in python. (I discussed this on IRC, and they suggest me to post here). I tried to split the string "walter ' cruz" using \b . In ruby, it returns: irb(main):001:0>"walter ' cruz".split(/\b/)
20
3748
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to do this. My tab delimited Input File looks like this:- 21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0 21 p 13d 63 124 580002 1160001 andrew -13.95 -13.37 0 0 21 p 12g 311 364 2900000 3385714 john -11.63 -11.14 0 0 21 q 11.1a 1274 1321...
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.