关于 JavaScript split() 函数的困惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5164883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:53:42  来源:igfitidea点击:

The confusion about the split() function of JavaScript

javascriptsplit

提问by hh54188

First I set a variable, and set it to empty:

首先我设置一个变量,并将其设置为空:

var str = "";

Then I split it through "&":

然后我通过“&”分割它:

var strs = str.split('&');

In the end, I show strs's length:

最后,我显示strs的长度:

alert( strs.length);

It alert "1".

它警告“1”。

But I assign nothing to the 'str' variable. Why does it still have a length, should't it be zero?

但是我没有给 'str' 变量赋值。为什么它还有长度,不应该是零吗?

回答by Martin Jespersen

From the MDC doc center:

来自 MDC 文档中心:

Note: When the string is empty, splitreturns an array containing one empty string, rather than an empty array.

注意:当字符串为空时,split返回一个包含一个空字符串的数组,而不是一个空数组。

Read the full docs here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

在此处阅读完整文档:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

In other words, this is by design, and not an error :)

换句话说,这是设计使然,而不是错误:)

回答by BoltClock

Because you get an array that contains the empty string:

因为你得到一个包含空字符串的数组:

[ "" ]

That empty string is one element. So lengthis 1.

那个空字符串是一个元素。length1也是如此。

回答by mahatmanich

Splitting window.location.pathname

拆分 window.location.pathname

Note that on window.location.pathname splitting it will mostly return a length of +1 also.

请注意,在 window.location.pathname 拆分时,它也将主要返回 +1 的长度。

Lets assume our pathname in this case is: /index.html.

让我们假设在这种情况下我们的路径名是:/index.html

var str = window.location.pathname.split('/');

It will be split into ["" , "index.html"]by design, as mentioned here many times before.

["" , "index.html"]正如之前多次提到的那样,它将按设计拆分。

What one could do in this case is, strip the leading and trailing /like so:

在这种情况下可以做的是,/像这样剥离前导和尾随:

var str = window.location.pathname.replace(/^\/|\/$/g, '').split('/');

and end up with the "correct"ed length.

并以“正确”的长度结束。

回答by jAndy

Description

The split method returns the new array.

When found, separator is removed from the string and the substrings are returned in an array. If separator is omitted, the array contains one element consisting of the entire string.

Note: When the string is empty, split returns an array containing one empty string, rather than an empty array.

描述

split 方法返回新数组。

找到后,从字符串中删除分隔符,并在数组中返回子字符串。如果省略了 separator ,则数组包含一个由整个字符串组成的元素。

注意:当字符串为空时,split 返回一个包含一个空字符串的数组,而不是一个空数组。

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

回答by Brian McGinity

I got sick of always checking for a[0] == '' so:

我厌倦了总是检查 a[0] == '' 所以:

String.prototype.splitPlus = function(sep) {
  var a = this.split(sep)
  if (a[0] == '') return [];
  return a;
};

Corrected version for when element 1 might be null:

元素 1 可能为空时的更正版本:

 String.prototype.splitPlus = function(sep) {
   var a = this.split(sep)
   if (a[0] == '' && a.length == 1) return [];
   return a;
  };

回答by KoolKabin

JavaScript splitcreates an array. That is, your variable, strs = [0]=>""and its length is 1.

JavaScriptsplit创建一个数组。也就是说,您的变量,strs = [0]=>""其长度为 1。

回答by bhargav 3vedi

try this

尝试这个

javascript gives two arrays by split function, then

javascript通过split函数给出两个数组,然后

var Val = "[email protected]";
var mail = Val.split('@');

if(mail[0] && mail[1])  {   alert('valid'); }
else    {   alert('Enter valid email id');  valid=0;    }

if both array contains length greater than 0 then condition will true

如果两个数组的长度都大于 0,则条件为真