473,734 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Behaviour of string.split with random input

I have this:

splitter = /[ -\/.]/;
dateItems = dateString.spli t (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 1537
Tim Streater wrote:
I have this:

splitter = /[ -\/.]/;
dateItems = dateString.spli t (splitter, 3);

where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.

[snip]

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

Perhaps?
Mick
Oct 21 '05 #2
In article <wf************ *******@twister .nyroc.rr.com>,
Mick White <mw***********@ rochester.rr.co m> wrote:
Tim Streater wrote:
I have this:

splitter = /[ -\/.]/;
dateItems = dateString.spli t (splitter, 3);

where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5.

[snip]

splitter = /[^\d]/;
dateItems = dateString.spli t(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************ *************** *****@individua l.net>,
dated Fri, 21 Oct 2005 12:27:24, seen in news:comp.lang. javascript, Tim
Streater <ti**********@d ante.org.uk> posted :
I have this:

splitter = /[ -\/.]/;
dateItems = dateString.spli t (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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
4348
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
9
2488
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 tried something like... int *Split(char *msg) { int len = 0; char *tmp; char *sub_string = NULL;
4
2315
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 = "\231-3423/\453-1234/\231-3473/\231-3474/" //randomly pick a phone num from string. n = randomNum;
3
2511
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 sqlite3 database table. Here's the context: The OMR card reader sends a stream of 69 bytes over the serial line; the last byte is a carriage return ('\r') indicating the end of record. Three pairs (in specific positions at the beginning of the...
12
3226
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
10
1782
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 matrix. A method named init() fills this matrix with random numbers. The main constructor requires an int param (fCosto) which is _not_used_ inside init(). Every time I instantiate a Soluzione object the matrix is filled by init(). The incredible...
9
4270
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 consist of only letters (A,C,G, or T) The number they input is the amount of 'random' implications applied to that String.. and replaces a letter in a random position with either (A,C,G, or T)
7
3492
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 character. For example: if the main string S is S='abcde fghc ijkl mnop'
20
3751
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
8776
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
9449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.