By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,973 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
<snip>
>
It's just the exact opposite of join().

Exactly right.
Yes... logically:

stringVar.split(delimiter).join(delimiter) <=stringVar
Oct 23 '08 #11

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.