且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串拆分返回一个数组,该数组包含比预期多的元素(空元素)

更新时间:2023-02-22 12:26:17

这是正确的预期行为.假设您已在字符串中包含分隔符,则split函数(已简化)将分隔符左侧的部分("a,b,c,d,e:10")作为第一个元素,并将该部分分隔符的其余部分(空字符串)作为第二个元素.

This is the correct and expected behavior. Given that you've included the separator in the string, the split function (simplified) takes the part to the left of the separator ("a,b,c,d,e:10") as the first element and the part to the rest of the separator (an empty string) as the second element.

如果您真的对split()的工作方式感到好奇,可以在http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

If you're really curious about how split() works, you can check out pages 148 and 149 of the ECMA spec (ECMA 262) at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf