473,405 Members | 2,171 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,405 software developers and data experts.

Behaviour of string.split with random input

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
that the dateItems array contains one item, itself a null string.

Is there something better to use/do in this context? It's completely
stupid to have to parse the string once to check it will not kill
..split, and then do the same again to extract the values I want.

(I get the error with Safari 1.3.1 and Netscape under XP).

Thanks,

-- tim
Oct 21 '05 #1
4 1515
Tim Streater wrote:
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.

[snip]

splitter = /[^\d]/;
dateItems = dateString.split(splitter,3);

Perhaps?
Mick
Oct 21 '05 #2
In article <wf*******************@twister.nyroc.rr.com>,
Mick White <mw***********@rochester.rr.com> wrote:
Tim Streater wrote:
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.

[snip]

splitter = /[^\d]/;
dateItems = dateString.split(splitter,3);


Thanks but actually I had been too hasty due to not debugging properly
to see where the script failed. In fact after the .split I had:

Month = dateItems[1];
Month = month.substr(0,3);

which is where it failed with "month has no properties". So I shouldn't
have blamed .split which was not at fault. Now I check that the array
has three items.

-- tim
Oct 21 '05 #3
Tim Streater wrote:
Thanks but actually I had been too hasty due to not debugging properly
to see where the script failed. In fact after the .split I had:

Month = dateItems[1]; ^ Month = month.substr(0,3); ^ ^ which is where it failed with "month has no properties". [...]
Now I check that the array has three items.


You are sure about your case here?
PointedEars
Oct 21 '05 #4
JRS: In article <ti********************************@individual.net >,
dated Fri, 21 Oct 2005 12:27:24, seen in news:comp.lang.javascript, Tim
Streater <ti**********@dante.org.uk> posted :
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
that the dateItems array contains one item, itself a null string.

Is there something better to use/do in this context?


Use a RegExp.

Read the newsgroup FAQ; see below; js-valid.htm, js-date4.htm .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 22 '05 #5

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

Similar topics

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. ...
9
by: Java and Swing | last post by:
Say I have a string which contains numbers separated by a comma... such as "0,1,2,3,4,5"...I want to split the string at the commas and return an array containing, 0,1...5. Suggestions? I've...
4
by: Aaron | last post by:
I like like to randomly output phone numbers seperated by \ and / from a string s = "\231-3423/\453-1234/\231-3473/\231-3474/" private string GetPhoneNum() { s =...
3
by: Rich Shepard | last post by:
I need to learn how to process a byte stream from a form reader where each pair of bytes has meaning according to lookup dictionaries, then use the values to build an array of rows inserted into a...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
10
by: Michele | last post by:
Please forgive me for the neverending code down here but I cannot find a rational explanation of the output of this simple program (really!). Soluzione class has a double field to represent a...
9
by: JimmyJava694 | last post by:
Hey everyone, I need help trying to mutate a String. For example.. I ask the person to input a number. Then I ask them to input a String. (The string they input can be any length, but must...
7
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th...
20
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.