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

S

Stevo

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+"<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+"<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+"<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+"<br>");

</script>
</body>
</html>
 
J

Joost Diepenmaat

Stevo said:
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?
 
G

Gregor Kofler

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
 
S

Stevo

Joost said:
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.
 
J

Joost Diepenmaat

Joost Diepenmaat said:
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.
 
J

Joost Diepenmaat

Stevo said:
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.
 
R

RoLo

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+"<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+"<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+"<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+"<br>");

</script>
</body>
</html>


Do people think this is logical behavior from the string split
method ?
people actually wrote the split method, so yes.
 
G

Gregor Kofler

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
 
E

Evertjan.

Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:
Yes... logically:

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

No, depending on extra constraints on 'delimiter'.

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

Gregor Kofler

Evertjan. meinte:
Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:


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
 
E

Evertjan.

Gregor Kofler wrote on 24 okt 2008 in comp.lang.javascript:
Evertjan. meinte:

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)
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Dan Rumney wrote on 24 okt 2008 in comp.lang.javascript:


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.
 
W

William James

Gregor Kofler wrote on 24 okt 2008 in comp.lang.javascript:







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)

This isn't exactly what you want, but it may be
of interest.

Using jslibs:

LoadModule('jsstd')

var my_string = "turmoil|tumult;inchoate|chaotic"
var regex = /([|;])/
var array = my_string.split( regex )
Print("length of array: ", array.length, "\n")
Print( array, "\n" )
var my_reconstruction = array.join("")
Print( my_reconstruction, "\n" )
Print( my_string === my_reconstruction )

--- output ---
length of array: 7
turmoil,|,tumult,;,inchoate,|,chaotic
turmoil|tumult;inchoate|chaotic
true

This trick also works in Ruby.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top