473,394 Members | 1,737 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,394 software developers and data experts.

Do people think this is logical behavior from the string split method?

If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat illogically?

Here's a test I just put together that splits on "&".
The test strings are:

"a&b" = (Correct!) I expect array length 2 and I get 2
"a&" = (Incorrect!) I expect array length 1 but I get 2
"a" = (Correct!) I expect array length 1 and get 1
"" = (Incorrect!) I expect array length 0 but I get 1

When I have an empty string or a trailing ampersand, then I get one too
many array entries.

<html>
<body>
<script>
var s="a&b";
var a=s.split("&");
document.write("test string='"+s+"'<br>");
document.write("a.length="+a.length+"<br>");
for(var i=0;i<a.length;i++)
document.write("a["+i+"]="+a[i]+"<br>");
document.write("<hr>");

var s="a&";
var a=s.split("&");
document.write("test string='"+s+"'<br>");
document.write("a.length="+a.length+"<br>");
for(var i=0;i<a.length;i++)
document.write("a["+i+"]="+a[i]+"<br>");
document.write("<hr>");

var s="a";
var a=s.split("&");
document.write("test string='"+s+"'<br>");
document.write("a.length="+a.length+"<br>");
for(var i=0;i<a.length;i++)
document.write("a["+i+"]="+a[i]+"<br>");
document.write("<hr>");

var s="";
var a=s.split("&");
document.write("test string='"+s+"'<br>");
document.write("a.length="+a.length+"<br>");
for(var i=0;i<a.length;i++)
document.write("a["+i+"]="+a[i]+"<br>");

</script>
</body>
</html>
Oct 23 '08 #1
14 1700
Stevo <no@mail.invalidwrites:
If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat illogically?

Here's a test I just put together that splits on "&".
The test strings are:

"a&b" = (Correct!) I expect array length 2 and I get 2
"a&" = (Incorrect!) I expect array length 1 but I get 2
"a" = (Correct!) I expect array length 1 and get 1
"" = (Incorrect!) I expect array length 0 but I get 1

When I have an empty string or a trailing ampersand, then I get one
too many array entries.
That's not incorrect. It's as documented. It's also the logical action
to take, considering the function is named "split", instead of
"tokenize": it splits the string into segments.

Quick quiz: what do you think "a&&b".split('&') should do, and why?
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Oct 23 '08 #2
Stevo meinte:
If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat illogically?
No. "a&" is "a" + "&" + "" - hence ["a", ""] as result makes perfect sense.

Gregor
Oct 23 '08 #3
Joost Diepenmaat wrote:
Stevo <no@mail.invalidwrites:
>If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat illogically?

Here's a test I just put together that splits on "&".
The test strings are:

"a&b" = (Correct!) I expect array length 2 and I get 2
"a&" = (Incorrect!) I expect array length 1 but I get 2
"a" = (Correct!) I expect array length 1 and get 1
"" = (Incorrect!) I expect array length 0 but I get 1

When I have an empty string or a trailing ampersand, then I get one
too many array entries.

That's not incorrect. It's as documented. It's also the logical action
to take, considering the function is named "split", instead of
"tokenize": it splits the string into segments.

Quick quiz: what do you think "a&&b".split('&') should do, and why?
What I would *want* it to do is give me an array of length 2 with "a" in
[0] and "b" in [1], but it's unlikely I'd get that. I expect I'll have
an entry in between with an empty string.
Oct 23 '08 #4
Joost Diepenmaat <jo***@zeekat.nlwrites:
Stevo <no@mail.invalidwrites:
>If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat illogically?

Here's a test I just put together that splits on "&".
The test strings are:

"a&b" = (Correct!) I expect array length 2 and I get 2
"a&" = (Incorrect!) I expect array length 1 but I get 2
"a" = (Correct!) I expect array length 1 and get 1
"" = (Incorrect!) I expect array length 0 but I get 1

When I have an empty string or a trailing ampersand, then I get one
too many array entries.

That's not incorrect. It's as documented. It's also the logical action
to take, considering the function is named "split", instead of
"tokenize": it splits the string into segments.

Quick quiz: what do you think "a&&b".split('&') should do, and why?
Just adding a thought: strictly splitting as split() does is a much more
useful operation, since you can deduce the input string from its output
(which means it's much easier to for example split a multiline string
on newlines and then separate it into paragraphs) and it's trivial to
ignore empty strings if you want to.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Oct 23 '08 #5
On Oct 23, 12:38*pm, Stevo <n...@mail.invalidwrote:
If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat illogically?

Here's a test I just put together that splits on "&".
The test strings are:

"a&b" = (Correct!) I expect array length 2 and I get 2
"a&" = (Incorrect!) I expect array length 1 but I get 2
"a" = (Correct!) I expect array length 1 and get 1
"" = (Incorrect!) I expect array length 0 but I get 1

When I have an empty string or a trailing ampersand, then I get one too
many array entries.

<html>
<body>
<script>
* *var s="a&b";
* *var a=s.split("&");
* *document.write("test string='"+s+"'<br>");
* *document.write("a.length="+a.length+"<br>");
* *for(var i=0;i<a.length;i++)
* * *document.write("a["+i+"]="+a[i]+"<br>");
* *document.write("<hr>");

* *var s="a&";
* *var a=s.split("&");
* *document.write("test string='"+s+"'<br>");
* *document.write("a.length="+a.length+"<br>");
* *for(var i=0;i<a.length;i++)
* * *document.write("a["+i+"]="+a[i]+"<br>");
* *document.write("<hr>");

* *var s="a";
* *var a=s.split("&");
* *document.write("test string='"+s+"'<br>");
* *document.write("a.length="+a.length+"<br>");
* *for(var i=0;i<a.length;i++)
* * *document.write("a["+i+"]="+a[i]+"<br>");
* *document.write("<hr>");

* *var s="";
* *var a=s.split("&");
* *document.write("test string='"+s+"'<br>");
* *document.write("a.length="+a.length+"<br>");
* *for(var i=0;i<a.length;i++)
* * *document.write("a["+i+"]="+a[i]+"<br>");

</script>
</body>
</html>
Do people think this is logical behavior from the string split
method ?
people actually wrote the split method, so yes.
Oct 23 '08 #6
Stevo <no@mail.invalidwrites:
What I would *want* it to do is give me an array of length 2 with "a"
in [0] and "b" in [1], but it's unlikely I'd get that. I expect I'll
have an entry in between with an empty string.
But if split did that, how would you name the function that behaves as
split() does now? In my experience, what split() does now is useful a
lot more often than your "do what I mean" variant.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Oct 23 '08 #7
RoLo wrote:
On Oct 23, 12:38 pm, Stevo <n...@mail.invalidwrote:
Do people think this is logical behavior from the string split
method ?
people actually wrote the split method, so yes.
lol, good one :)
Oct 23 '08 #8
Joost Diepenmaat meinte:
Just adding a thought: strictly splitting as split() does is a much more
useful operation, since you can deduce the input string from its output
(which means it's much easier to for example split a multiline string
on newlines and then separate it into paragraphs) and it's trivial to
ignore empty strings if you want to.
It's just the exact opposite of join().

Gregor

Oct 23 '08 #9
Gregor Kofler <us****@gregorkofler.atwrites:
Joost Diepenmaat meinte:
>Just adding a thought: strictly splitting as split() does is a much more
useful operation, since you can deduce the input string from its output
(which means it's much easier to for example split a multiline string
on newlines and then separate it into paragraphs) and it's trivial to
ignore empty strings if you want to.

It's just the exact opposite of join().
Exactly right.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Oct 23 '08 #10
<snip>
>
It's just the exact opposite of join().

Exactly right.
Yes... logically:

stringVar.split(delimiter).join(delimiter) <=stringVar
Oct 23 '08 #11
Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:
<snip>
>>
It's just the exact opposite of join().

Exactly right.

Yes... logically:

stringVar.split(delimiter).join(delimiter) <=stringVar
No, depending on extra constraints on 'delimiter'.

split() can be used with regex, join() cannot.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 24 '08 #12
Evertjan. meinte:
Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:
><snip>
>>>It's just the exact opposite of join().
Exactly right.
Yes... logically:

stringVar.split(delimiter).join(delimiter) <=stringVar

No, depending on extra constraints on 'delimiter'.

split() can be used with regex, join() cannot.
Perhaps they would have put in regex as delimiter for join(), if that
made any sense.

Gregor
Oct 24 '08 #13
Gregor Kofler wrote on 24 okt 2008 in comp.lang.javascript:
Evertjan. meinte:
>Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:
>><snip>
It's just the exact opposite of join().
Exactly right.

Yes... logically:

stringVar.split(delimiter).join(delimiter) <=stringVar

No, depending on extra constraints on 'delimiter'.

split() can be used with regex, join() cannot.

Perhaps they would have put in regex as delimiter for join(), if that
made any sense.
Well it would definitely have been nice to have an array !! of the
different strings that matched the regexed split regex.

var contentArray = myString.split(/\d+/)

var splitArray = regExp.lastSplitMatchesArray

(contentArray.length - 1 === splitArray.length)

myReconstruction = contentArray.join(splitArray)

(myString === myReconstruction)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 24 '08 #14
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Fri, 24 Oct 2008 07:12:38, Evertjan. <ex**************@interxnl.net>
posted:
>Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:
><snip>
>>>
It's just the exact opposite of join().

Exactly right.

Yes... logically:

stringVar.split(delimiter).join(delimiter) <=stringVar

No, depending on extra constraints on 'delimiter'.

split() can be used with regex, join() cannot.
A RegExp is not a delimiter; it is a descriptor of a class of
delimiters.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Oct 25 '08 #15

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

Similar topics

11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
2
by: Antoon Pardon | last post by:
I was wondering what people whould think about a change of behaviour in the split method fo strings. The idea would be that if maxsplit was negative then abs(maxsplit) splits would be done, but...
4
by: Henry Chen | last post by:
Hi, I have a string that needs to be parsed into the string. The separator is not char. It is something like " at ". With current string.Split function, it doesn't work. Is there any exist...
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
1
by: Prasad Dabak | last post by:
Hello, I have a legacy unmanaged application that returns property=value pairs separated by chr(252)and I am trying to parse this output from C# using string.split method. This works fine as...
4
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
5
by: chindanoor | last post by:
As i used split method in my local Machine(jdk1.4) it was working fine.., When i try to use in the server(which is jdk1.3 version) unable to support it.., As i m able to do it with StringTokenizer...
7
by: John A Grandy | last post by:
Is string.Split() performant to a degree that there's no point in trying to write code to improve on its performance ?
3
by: DaveRook | last post by:
Hi I've had a few error messages with this I don't understand why. It works fine in a Windows Form, but now moving it to a website, it's not working. I don't have the option to count the lines as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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...
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...

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.