search

Home  >  Q&A  >  body text

javascript - When the value of String is the same as String.split('') parameter one and is not empty, the length of the split array is greater than 0?

When the value of String is the same as String.split('') parameter 1 and is not empty, the length of the split array is 2. Use String.substr(1).split('') to split the array. The array length is 1
For example:

let a = ','
a.split(',')
//返回结果 ["",""], a.length为2
a.substr(1).split(',')
//返回结果 [""], a.length为1
a = ''
a.split('') 
//返回结果[], a.length为0
伊谢尔伦伊谢尔伦2749 days ago533

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-19 10:32:28

    You don’t understand String.prototype.substr().

    a.substr(1) means: starting from index 1 (the second position) and ending at the end of the string.
    So what we get here is the null character.

    So split did not match and returned itself (empty string).

    reply
    0
  • Cancelreply