人们认为这是字符串拆分方法的逻辑行为吗? [英] Do people think this is logical behavior from the string split method?

查看:58
本文介绍了人们认为这是字符串拆分方法的逻辑行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用split方法将字符串拆分为数组,那么就不会像我期望的那样工作。这并不意味着它的价值是错误的,但是其他人是否同意这种做法有点不合逻辑?


这里'' sa测试我把它放在一起分成&。

测试字符串是:


" a& b" =(正确!)我希望数组长度为2,我得到2

" a&" =(不正确!)我希望数组长度为1,但我得到2

a =(正确!)我希望数组长度为1并得到1

"" =(不正确!)我希望数组长度为0,但我得到1


当我有一个空字符串或尾随的&符号时,我也得到一个

许多数组条目。


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

文件。 write(a.length =" + a.length +< br>");

for(var i = 0; i< a.length; i ++)

document.write(" a [" + i +"] =" + a [i] +"< br>");


< / script>

&l t; / body>

< / html>

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>

推荐答案

Stevo< no@mail.invalidwrites:
Stevo <no@mail.invalidwrites:

如果你使用split方法将一个字符串拆分成一个数组,那就不会像我期望的那样工作
。这并不意味着它的价值是错误的,但是其他人是否同意这种做法有点不合逻辑?


这里'' sa测试我把它放在一起分成&。

测试字符串是:


" a& b" =(正确!)我希望数组长度为2,我得到2

" a&" =(不正确!)我希望数组长度为1,但我得到2

a =(正确!)我希望数组长度为1并得到1

"" =(不正确!)我希望数组长度为0但我得到1


当我有一个空字符串或尾随的&符号时,我得到一个

许多数组条目。
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.



这不是不正确的。这是有记录的。这也是逻辑行动

,考虑到函数名为split,而不是

" tokenize":它将字符串拆分为段。


快速测验:您认为a&& b.split(''&'')应该做什么,为什么?

-

Joost Diepenmaat |博客: http://joost.zeekat.nl/ |工作: http://zeekat.nl/

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/

Stevo meinte:
Stevo meinte:

如果使用split方法将字符串拆分为数组,则不是

工作我希望它的方式。这并不意味着它的价值是错误的,但其他人是否同意这种做法有点不合逻辑?
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&是a +"&" + - 因此[a,"]结果非常有意义。


Gregor

No. "a&" is "a" + "&" + "" - hence ["a", ""] as result makes perfect sense.

Gregor


Joost Diepenmaat写道:
Joost Diepenmaat wrote:

Stevo< no@mail.invalidwrites:
Stevo <no@mail.invalidwrites:

>如果你将一个字符串拆分成使用split方法的数组,它并不像我期望的那样工作。那并不意味着它当然是错的,但是其他人是否同意它的工作有点不合逻辑?

这里是一个测试我只是放在一起分裂在&上。
测试字符串是:

< a& b" =(正确!)我希望数组长度为2,我得到2
a& =(不正确!)我希望数组长度为1,但我得到2
a =(正确!)我希望数组长度为1并得到1
" =(不正确!)我希望数组长度为0,但我得到1

当我有一个空字符串或尾随的&符号时,我得到一个
太多的数组条目。
>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.



这不是不正确的。这是有记录的。这也是逻辑行动

,考虑到函数名为split,而不是

" tokenize":它将字符串拆分为段。


快速测验:您认为a&& b.split(''&'')应该做什么,为什么?


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?



我想*它要做的是给我一个长度为2的数组,其中a为a。在

[0]和b中在[1]中,但我不太可能得到它。我希望我能用空字符串输入


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.


这篇关于人们认为这是字符串拆分方法的逻辑行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆