简体   繁体   English

JavaScript 拆分为空字符串提供数组大小为 1 而不是零

[英]JavaScript split gives array size one instead of zero for empty string

I want to split a string by comma to get an array in Node.js.我想用逗号分割一个字符串以在 Node.js 中获得一个数组。

exports.test = function(rq, rs){

   var mailList = "sharan@test.com,pradeep@test.com";
   var arrayList = mailList.split(",");    
   console.log(mailList + " array lenght " + arrayList.length );

   mailList = "";
   arrayList = mailList.split(",");
   console.log(mailList + " array lenght " + arrayList.length );

   mailList = "sharan@test.com,";
   console.log(mailList + " array lenght " + arrayList.length );

   mailList = ",";
   console.log(mailList + " array lenght " + arrayList.length );

   rs.send("test here ");

}

The console output is:控制台输出是:

sharan@test.com,pradeep@test.com array lenght 2
array lenght 1
sharan@test.com, array lenght 1
, array lenght 1

Why does the JavaScript "".split() return an array with one element instead of an empty array?为什么 JavaScript "".split()返回一个包含一个元素的数组而不是一个空数组?

The returned array contains a single empty string ( [""] ).返回的数组包含一个空字符串 ( [""] )。 It works this way because everything up until the first match (or end of string) is returned as the first element of the array.它以这种方式工作,因为直到第一个匹配项(或字符串结尾)为止的所有内容都作为数组的第一个元素返回。 In the case of the empty string, this is an empty string.在空字符串的情况下,这是一个空字符串。

If you think about how an implementation of the split algorithm might look like, this makes sense.如果您考虑拆分算法的实现可能是什么样子,这是有道理的。 Probably you start with an empty string containing the current element, and then you loop through the letters of the string adding them to the current element until you get to the end of the string or a separator.可能您从包含当前元素的空字符串开始,然后遍历字符串的字母,将它们添加到当前元素,直到到达字符串的末尾或分隔符。 Then you push the current element onto the results array.然后将当前元素推送到结果数组上。

In the case where you have a zero length string, you start with an empty string for the current element.如果您的字符串长度为零,则从当前元素的空字符串开始。 You directly reach the end, so you push the empty string to the results array an return it.您直接到达末尾,因此您将空字符串推送到结果数组并返回它。

This is also how it is supposed to work.这也是它应该如何工作。 From the ECMAScript Language Specification :来自ECMAScript 语言规范

If the this object is (or converts to) the empty String, the result depends on whether separator can match the empty String.如果 this 对象是(或转换为)空字符串,则结果取决于分隔符是否可以匹配空字符串。 If it can, the result array contains no elements.如果可以,则结果数组不包含任何元素。 Otherwise, the result array contains one element, which is the empty String.否则,结果数组包含一个元素,即空字符串。

And from Mozilla :来自Mozilla

When found, separator is removed from the string and the substrings are returned in an array.找到后,从字符串中删除分隔符,并在数组中返回子字符串。 If separator is not found or 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()返回一个包含一个空字符串的数组,而不是一个空数组。

If you dislike the behavior, you can write your own version:如果您不喜欢这种行为,可以编写自己的版本:

//Return an empty array if string is empty.
//Otherwise return the result of the ordinary split.
split2 = (separator) => this == "" ? [] : this.split(separator);

Everything before the first match is returned as the first element.第一次匹配之前的所有内容都作为第一个元素返回。 Even if the String is Empty.即使字符串为空。 It's not null它不是空的

If you want split and return an 0 length Array, I recommand you to use the underscore.string module and the words method :如果你想拆分并返回一个长度为 0 的数组,我建议你使用underscore.string 模块words方法:

_str.words("", ",");
// => []

_str.words("Foo", ",");
// => [ 'Foo' ]

One way to do is to check whether the first value of the split array is empty or not一种方法是检查拆分数组的第一个值是否为空

var arrayList = mailList.split(",");
if(arrayList[0] != ""){
  arrayLength = arrayList.length;
}else{
  arrayLength = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM