简体   繁体   English

有关JavaScript String.prototype.substring的问题

[英]Question regarding JavaScript String.prototype.substring

 var alpha = "ABCDEFG"; var result = alpha.substring(0, 5); document.write(result); 

The question is, why the answer is ABCDE(0,4) instead of ABCDEF (0,5). 问题是,为什么答案是ABCDE(0,4)而不是ABCDEF(0,5)。 All numbers in programming start with zero, right? 编程中的所有数字都以零开头,对吗?

String.prototype.substring takes up to two arguments. String.prototype.substring最多包含两个参数。 One for the start index one for the end index (optional). 一个代表开始索引,一个代表结束索引(可选)。 While the starting index is included in the new string, the end index is excluded. 当起始索引包含在新字符串中时,结束索引将被排除。 When 4 is the end index that character will be excluded and you will see characters 0-3 当4是结束索引时,该字符将被排除,您将看到字符0-3

Please read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring 请阅读https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

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

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