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